Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Object is not typed correctly when using allOf. #329

Closed
michalsrutek opened this issue Jan 30, 2022 · 3 comments · Fixed by #332
Closed

[BUG] Object is not typed correctly when using allOf. #329

michalsrutek opened this issue Jan 30, 2022 · 3 comments · Fixed by #332
Assignees
Labels
bug Something isn't working Triage needed

Comments

@michalsrutek
Copy link
Contributor

Describe the bug
Object is not typed correctly when using allOf.

To Reproduce

Let's say I have the following swagger.

{
    "openapi": "3.0.3",
    "info": {
        "title": "Test API",
        "version": "1.0.0"
    },
    "paths": {
        "/test": {
            "get": {
                "operationId": "testRetrieve",
                "responses": {
                    "200": {
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Test"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Customer": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "readOnly": true
                    },
                    "email": {
                        "type": "string",
                        "format": "email",
                        "readOnly": true
                    }
                },
                "required": [
                    "email",
                    "name"
                ]
            },
            "Test": {
                "type": "object",
                "properties": {
                    "customer": {
                        "allOf": [
                            {
                                "$ref": "#/components/schemas/Customer"
                            }
                        ],
                        "readOnly": true
                    }
                },
                "required": [
                    "customer"
                ]
            }
        }
    }
}

This produces the following code.

@JsonSerializable(explicitToJson: true)
class Test {
  Test({
    this.customer,
  });

  factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);

  @JsonKey(name: 'customer')
  final dynamic customer;

  ...
}

Note that the customer property is not typed.

Expected behavior

I expect the customer property to be typed, as shown below.

@JsonSerializable(explicitToJson: true)
class Test {
  Test({
    this.customer,
  });

  factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);

  @JsonKey(name: 'customer')
  final Customer? customer;

  ...
}

Swagger specification link
Link to swagger/OpenApi documentation

Library version used:
2.3.10

@Vovanella95
Copy link
Collaborator

Hi @michalsrutek

Thanks for raising issue. I will fix your issue, but pay attention, that actually, customer is not Customer. If 'allOf' has not only ref, but additional properties - we should create special class, contains all fields of Customer + all properties, mentioned in it.

So generator will generate Customer when there are no properties inside of allOf, and dynamic in cases, when you add it, for example, like this:

Test: type: object properties: customer: allOf: - $ref: '#/components/schemas/Customer' - properties: id: type: string x-annotation-builtinType: string x-annotation-inherited: true readOnly: true required: - customer

@Vovanella95
Copy link
Collaborator

@michalsrutek enjoy 2.3.12 version :)

@michalsrutek
Copy link
Contributor Author

@Vovanella95 Perfect. Thank you for your amazing work! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Triage needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants