swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.02k stars 6.03k forks source link

spring: add annotation if property name endsWith("FOO")> #4877

Open adhamhf opened 7 years ago

adhamhf commented 7 years ago
Description
Swagger-codegen version

2.2.2-SNAPSHOT

Help/feature request

I'm writing a new generator for Spring and JSON-API. I'm using the https://github.com/jasminb/jsonapi-converter to generate JSON-API from our models.

JSON-API requires an ID in the payload, the jsonapi-converter uses its @Id annotation to specify which field should be used as the ID in the payload.

Currently I have this hacked solution in place:

        if( property.baseName.endsWith("FOO") ) {
            property.example = "true";
        }

    {{#example}}@Id{{/example}}
    private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};

Is there a better way to do this?

Thanks!

wing328 commented 7 years ago

@adhamhf not sure if I understand it correctly, do you mean JSON-API requires @Id in all the payload/object?

adhamhf commented 7 years ago

@wing328 yes, it requires id ( not @id that's just the java annotation ) when you are reading, updating or deleting objects. It is not required for creating new objects.

wing328 commented 7 years ago

@adhamhf another way I can think of is using vendor extensions with customized templates.

  1. Add vendor extension to the property in the spec (e.g. "x-jsonapi-id")
  2. Customize the template as follows
    {{#example}}@Id{{/example}}
    private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};
  3. Use the customized templates with the "-t" command