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
16.93k stars 6.03k forks source link

refer to properties in $ref get Compilation error in generated class although it is valid swagger yml #9167

Open shockey opened 5 years ago

shockey commented 5 years ago

From @noha-gamal on February 12, 2019 13:56

Q&A (please complete the following information)

Content & configuration

Example Swagger/OpenAPI definition:

swagger: '2.0'
host: api.vodafone.com
basePath: '/{access}/eligibleProductOfferingAPI/v2'
schemes:
  - https
consumes:
  - application/json
produces:
  - application/json
info:
  title: 'Common Service Model API: EligibleProductOffering'
  version: 2.1.0
paths:
  /eligibleProductOffering:
    get:
      tags:
        - eligibleProductOffering
      description: >-
        A method that allows a consumer of the API to get-list a
        EligibleProductOffering representation.
      operationId: getList
      produces:
        - application/json
      responses:
        '204':
          description: '204, No Content.'
definitions:
  eligibleProductOfferingVBOProductOffering:
    properties:
      id:
        type: array
        minItems: 1
        items:
          $ref: '#/definitions/BasicComponents/properties/IDType'
      name:
        type: string
        description: ' The name of the instance of a business object or component.'
  BasicComponents:
    properties:
      IDType:
        type: object
        properties:
          value:
            type: string
            description: The value of the field.
          schemeID:
            type: string
          schemeName:
            type: string
          schemeAgencyName:
            type: string
        required:
          - value

Describe the bug you're encountering

Bug is in class EligibleProductOfferingVBOProductOffering.java get id attribute private List id = null; where class BasicComponentspropertiesIDType not found. expected attribute id with type BasicComponentsIDType

To reproduce...

Steps to reproduce the behavior:

  1. Go to 'https://editor.swagger.io/'
  2. Paste above yml 4.Click on Generate Server then Spring 5.Spring-server-generated.zip downloaded, open it and go to spring-server\src\main\java\io\swagger\model\EligibleProductOfferingVBOProductOffering.java
  3. Found compilation error in class generated.

Expected behavior

expected that id attribute in EligibleProductOfferingVBOProductOffering.java be private List id = null; instead of private List id = null;

Copied from original issue: swagger-api/swagger-ui#5175

JulianeH commented 5 years ago

I do have also a problem with generating the class name with Swagger-codegen 3.0.5. In my case I have different folders for parameter and definitions. When I'd like to address the $ref from another folder it is combining the full path of the $ref as class attribute:

swagger.yml:

openapi: 3.0.0
...
  /alerts/query:
    post:
...
      requestBody:
        $ref: ./parameters/alerts.yml#/AlertsQueryParameter
...

parameters/alerts.yml:

AlertsQueryParameter:
  name: alertQueryPayload
  content:
    application/json:
      schema:
        $ref: ./definitions/alerts.yml#/AlertsQuery
...

definitions/alerts.yml:

AlertsQuery:
  type: object
  properties:
    pageable:
      $ref: "./app.yml#/Pageable"
...

definitions/app.yml:

Pageable:
  type: object
  properties:
    pageNumber:
      type: integer
      format: int64
    pageSize:
      type: integer
      format: int64
    sortingInstructions:
      type: array
      items:
      $ref: '#/SortingInstruction'
  required:
    - pageNumber
    - pageSize
    - sorts

The name of the class should be Pageable but it is creating DefinitionsappYmlPageable

vithu30 commented 4 years ago

@shockey, I'm also facing the similar issue when using swagger-codegen 3.x. Is there any way to overcome this? Compilation Error in the generated code when there is reference in the format:

$ref: '#/components/schemas/entry'

In the generated service code, it imports a class called ComponentsSchemasEntryModel which makes the compilation error. I am using swagger-codegen-maven-plugin version 3.0.19 with language jaxrs-cxf-cdi