AdevintaSpain / Protein

💊 Protein is an IntelliJ Plugin to generate Kotlin code for Retrofit 2 and RxJava 2 based on a Swagger definition
https://plugins.jetbrains.com/plugin/10206-protein--android-component-builder
Apache License 2.0
274 stars 8 forks source link

Protein fails to generate proper code when objects have dots in their name #29

Open MichalDanielDobrzanski opened 5 years ago

MichalDanielDobrzanski commented 5 years ago

Describe the bug Protein is a really cool tool, I like it! However, it has failed to generate right names for data classes. Generated classes are like "data class Class.V1.API.Model", and their names are put in Kotlin captions ``.

To Reproduce Just add dots in the object names in JSON file. Example:

  "Company.MyAwesomeApi.Contracts.V1.Specific.SecondSpecific": {
            "type": "object",
            "properties": {
                "date": {
                    "format": "date-time",
                    "type": "string"
                },
                "extendedPropertyTypeId": {
                    "format": "uuid",
                    "type": "string"
                },
                "value": {
                    "type": "string"
                }
            }
        },

Expected behavior The easiest way would be to trim dots and generate clients with CapitalCase naming convenetion (or camelCase): CompanyMyAwesomeApiContractsV1SpecificSecondSpecific The JSON file cannot be changed since it is generated via .NET code and they use dots for namespaces.

Info Required

Screenshots

Additional context

ferranpons commented 5 years ago

Hi @MichalDanielDobrzanski

I'm checking the problem you describe but I'm not sure if it's a generator problem or a definition problem. I explain, if the definition must be the final name of the class why you define it with dots instead of using the correct naming instead? If that swagger definition is auto-generated from the .NET backend service may it be corrected there IMHO. You want me to make a change to fit your needs when you should change how you name those objects. Is more like "linting" the JSON than changing the tool.

We can debate it if you're not agree.

yudhir commented 5 years ago
"\/api\/sections\/{id}": {
            "get": {
                "tags": [
                    "Section"
                ],
                "operationId": "getSectionItem",
                "produces": [
                    "application\/ld+json",
                    "application\/json",
                    "text\/html",
                    "application\/hal+json",
                    "multipart\/form-data"
                ],
                "summary": "Retrieves a Section resource.",
                "parameters": [{
                    "name": "id",
                    "in": "path",
                    "required": true,
                    "type": "string"
                }],
                "responses": {
                    "200": {
                        "description": "Section resource response",
                        "schema": {
                            "$ref": "#\/definitions\/Section-section.read"
                        }
                    },
                    "404": {
                        "description": "Resource not found"
                    }
                }
            },

here is a snippet.

@ApiModel
final class Section-section.read { Error expecting top level declaration, 

  @ApiModelProperty
  @SerializedName("name")          Error Functio declaration must have a name
  private var name: String```

@DELETE("/api/sections/{id}") fun deleteSectionItem(): Observable Expecting Member declaration

ferranpons commented 5 years ago

Hi @yudhir !

Why you named a reference that way? Since a reference should be a model from your definition, is there any reason for that?

Please could you post the definition of Section-section.read? Is auto-generated from an external service?

Thanks!

yudhir commented 5 years ago

yes it was autogenerated from api-platform and probably a result of intenal Joins and Serialization and Normalization rules. here is my swagger

 "Section-section.read": {
            "type": "object",
            "description": "The most generic type of item.",
            "properties": {
                "name": {
                    "description": "the name of the item",
                    "type": "string"
                },
                "image": {
                    "description": "An image of the item. This can be a \\[\\[URL\\]\\] or a fully described \\[\\[ImageObject\\]\\].",
                    "type": "string"
                },
                "description": {
                    "description": "a description of the item",
                    "type": "string"
                },
                "articles": {
                    "type": "array",
                    "items": {
                        "$ref": "#\/definitions\/Article-section.read"
                    }
                },
                "newsArticles": {
                    "type": "array",
                    "items": {
                        "$ref": "#\/definitions\/NewsArticle-section.read"
                    }
                }
            }
        },

OpenaApi gen i think used SectionMinusSectionRead to get around this . I need an Rx, Retrofit solution , that is not available on the openapi-gen and swagger-gen .