swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Fixed generating class for properties with empty properties

Open korkut89 opened this issue 8 years ago • 11 comments

Codegen doesn't generate class when an object defined with an empty properties list. When an object named risk property defined with empty properties list generator defines it as java.lang.Object. But when reading the object with ObjectMapper it doesn't read risk : {}.

PR checklist

  • [x] Read the contribution guidelines.
  • [ ] Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • [x] Filed the PR against the correct branch: 3.0.0 branch for changes related to OpenAPI spec 3.0. Default: master.
  • [ ] Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

swagger: '2.0'
info:
  title: Account and Transaction API Specification
  description: Swagger for Account and Transaction API Specification
  termsOfService: https://www.openbanking.org.uk/terms
  contact:
    name: Service Desk
    email: [email protected]
  license:
    name: open-licence
    url: https://www.openbanking.org.uk/open-licence
  version: v1.1.0
basePath: /open-banking/v1.1
schemes:
  - https
produces:
  - application/json; charset=utf-8
paths:
  /hello:
    post:
      summary: Say hello
      description: Say hello
      operationId: sayHello
      consumes:
      - application/json; charset=utf-8;
      produces:
      - application/json; charset=utf-8;
     parameters:
        - in : body
        name: body
        description: Setup to say hello
        required: true
        schema:
          title: Hello setup POST request
          description: Allows a hello setup
          type: object
          properties:
            Data:
              description: ''
              title: Hello Setup
              type: object
              properties:
                Greeting:
                  description: 'greeting'
                  type: string
              required:
              -  Greeting
              additionalProperties: false
            Risk:
              description: ''
              title: Risk
              type: object
              properties: {}
              additionalProperties: false
          required:
          - Data
          - Risk
          additionalProperties: false
      responses:
        201:
          description: Greet back
          schema:
            title: Greet back POST response
            type: object
            properties:
              Helloback:
                description: Hello back
                type: string

Codegen does not generate a class for Risk. And generates the HelloSetupPOSTRequest with variables Data data and java.lang.Object Risk.

korkut89 avatar Dec 11 '17 15:12 korkut89

Codegen doesn't generate class when an object defined with an empty properties list

Can you share more about the use case with an empty properties list?

wing328 avatar Dec 12 '17 15:12 wing328

I added an example yaml file @wing328

korkut89 avatar Dec 13 '17 07:12 korkut89

@korkut89 sorry I mean why an object is defined without properties? I think only a handful of programming languages support empty object (an object without property)

What does the corresponding JSON payload look like?

wing328 avatar Dec 14 '17 16:12 wing328

{
    "Data" : {
        "Greeting" : "Hi"
    },
    "Risk": {}
}

It should look something like that.

korkut89 avatar Dec 15 '17 07:12 korkut89

@korkut89 thanks for sharing more. Given that the JSON payload is well-defined, why define it as an empty object?

wing328 avatar Dec 15 '17 16:12 wing328

@wing328 https://github.com/OpenBankingUK/account-info-api-spec/blob/master/dist/v1.1/account-info-swagger.yaml We are using this spec. Since this spec defined as standard for open banking we must use this. I am not sure why it was defined as an empty object.

korkut89 avatar Dec 18 '17 09:12 korkut89

@korkut89 I think the spec owner will need to update the spec to have a proper definition of the model with valid properties instead.

wing328 avatar Dec 27 '17 02:12 wing328

@wing328 well, although weird, this spec is valid. So we should generate something correct. The filter on the properties size was introduced by #2297 so the impact of changing it has to be carefully studied as regards to the issue it was solving.

cbornet avatar Dec 28 '17 09:12 cbornet

I think this spec is likely to be updated in the long run. But can't say for certain that it is going to change. I agree with @cbornet this is a valid spec.

korkut89 avatar Dec 28 '17 11:12 korkut89

I agreed with you guys the spec is valid according to the specification.

My point is without knowing how the object (JSON, XML) is defined, how can any developers figure out how to process the object?

The issue is nothing swagger-codegen-specified. Someone who is manually writing the client will also encounter the same issue.

wing328 avatar Jan 05 '18 09:01 wing328

To me, Risk should be a class with no attributes, no ?

cbornet avatar Jan 19 '18 23:01 cbornet