swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
785 stars 531 forks source link

Invalid reference mapping for schema when parser has enabled with `flatten ` #1859

Open lnash94 opened 1 year ago

lnash94 commented 1 year ago

I'm using swagger parser 03, with flatten enable

ParseOptions parseOptions = new ParseOptions();
parseOptions.setFlatten(true); 
final OpenAPI openAPI = new OpenAPIV3Parser().read(openAPIFileContent, null, parseOptions);
...

The Received OpenAPI model contains mismatching schema names with its references.

$ref: #/components/schemas/inline_response_200
Schema name: Inline_response_200
Below is the sample example I tried 
```openapi
openapi: 3.0.1
info:
  title: Sample API
  description: API description in Markdown.
  version: 1.0.0
servers:
  - url: 'https://api.example.com'
paths:
  /store/inventory:
    get:
      tags:
        - store
      operationId: getInventory
      responses:
        "200":
          description: successful operation
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                  age:
                    type: integer
                additionalProperties:
                  type: integer
                  format: int32
  /store/inventory02:
    get:
      tags:
        - store
      operationId: getInventory02
      responses:
        "200":
          description: successful operation
          content:
            application/json:
              schema:
                type: object
                additionalProperties:
                  type: object
                  properties:
                    id:
                     type: integer
                    age:
                      type: integer
            text/plain:
              schema:
                type: string

The OpenAPI model received contains mismatching schema names with references.

$ref: #/components/schemas/inline_response_200
Schema name: Inline_response_200

OpenAPI model received for above yaml file

class OpenAPI {
    openapi: 3.0.1
    info: class Info {
        title: allOF
        description: API description in Markdown.
        termsOfService: null
        contact: null
        license: null
        version: 1.0.0
    }
    externalDocs: null
    servers: [class Server {
        url: https://api.example.com
        description: null
        variables: null
    }]
    security: null
    tags: null
    paths: class Paths {
        {/store/inventory=class PathItem {
            summary: null
            description: null
            get: class Operation {
                tags: [store]
                summary: null
                description: null
                externalDocs: null
                operationId: getInventory
                parameters: null
                requestBody: null
                responses: class ApiResponses {
                    {200=class ApiResponse {
                        description: successful operation
                        headers: null
                        content: class Content {
                            {application/json=class MediaType {
                                schema: class Schema {
                                    type: null
                                    format: null
                                    $ref: #/components/schemas/inline_response_200
                                    description: null
                                    title: null
                                    multipleOf: null
                                    maximum: null
                                    exclusiveMaximum: null
                                    minimum: null
                                    exclusiveMinimum: null
                                    maxLength: null
                                    minLength: null
                                    pattern: null
                                    maxItems: null
                                    minItems: null
                                    uniqueItems: null
                                    maxProperties: null
                                    minProperties: null
                                    required: null
                                    not: null
                                    properties: null
                                    additionalProperties: null
                                    nullable: null
                                    readOnly: null
                                    writeOnly: null
                                    example: null
                                    externalDocs: null
                                    deprecated: null
                                    discriminator: null
                                    xml: null
                                }
                                examples: null
                                example: null
                                encoding: null
                            }}
                        }
                        links: null
                        extensions: null
                        $ref: null
                    }}
                    extensions: null
                }
                callbacks: null
                deprecated: null
                security: null
                servers: null
            }
            put: null
            post: null
            delete: null
            options: null
            head: null
            patch: null
            trace: null
            servers: null
            parameters: null
            $ref: null
        }, /store/inventory02=class PathItem {
            summary: null
            description: null
            get: class Operation {
                tags: [store]
                summary: null
                description: null
                externalDocs: null
                operationId: getInventory02
                parameters: null
                requestBody: null
                responses: class ApiResponses {
                    {200=class ApiResponse {
                        description: successful operation
                        headers: null
                        content: class Content {
                            {application/json=class MediaType {
                                schema: class MapSchema {
                                    class Schema {
                                        type: object
                                        format: null
                                        $ref: null
                                        description: null
                                        title: null
                                        multipleOf: null
                                        maximum: null
                                        exclusiveMaximum: null
                                        minimum: null
                                        exclusiveMinimum: null
                                        maxLength: null
                                        minLength: null
                                        pattern: null
                                        maxItems: null
                                        minItems: null
                                        uniqueItems: null
                                        maxProperties: null
                                        minProperties: null
                                        required: null
                                        not: null
                                        properties: null
                                        additionalProperties: class Schema {
                                            type: null
                                            format: null
                                            $ref: #/components/schemas/inline_response_map200
                                            description: null
                                            title: null
                                            multipleOf: null
                                            maximum: null
                                            exclusiveMaximum: null
                                            minimum: null
                                            exclusiveMinimum: null
                                            maxLength: null
                                            minLength: null
                                            pattern: null
                                            maxItems: null
                                            minItems: null
                                            uniqueItems: null
                                            maxProperties: null
                                            minProperties: null
                                            required: null
                                            not: null
                                            properties: null
                                            additionalProperties: null
                                            nullable: null
                                            readOnly: null
                                            writeOnly: null
                                            example: null
                                            externalDocs: null
                                            deprecated: null
                                            discriminator: null
                                            xml: null
                                        }
                                        nullable: null
                                        readOnly: null
                                        writeOnly: null
                                        example: null
                                        externalDocs: null
                                        deprecated: null
                                        discriminator: null
                                        xml: null
                                    }
                                }
                                examples: null
                                example: null
                                encoding: null
                            }, text/plain=class MediaType {
                                schema: class StringSchema {
                                    class Schema {
                                        type: string
                                        format: null
                                        $ref: null
                                        description: null
                                        title: null
                                        multipleOf: null
                                        maximum: null
                                        exclusiveMaximum: null
                                        minimum: null
                                        exclusiveMinimum: null
                                        maxLength: null
                                        minLength: null
                                        pattern: null
                                        maxItems: null
                                        minItems: null
                                        uniqueItems: null
                                        maxProperties: null
                                        minProperties: null
                                        required: null
                                        not: null
                                        properties: null
                                        additionalProperties: null
                                        nullable: null
                                        readOnly: null
                                        writeOnly: null
                                        example: null
                                        externalDocs: null
                                        deprecated: null
                                        discriminator: null
                                        xml: null
                                    }
                                }
                                examples: null
                                example: null
                                encoding: null
                            }}
                        }
                        links: null
                        extensions: null
                        $ref: null
                    }}
                    extensions: null
                }
                callbacks: null
                deprecated: null
                security: null
                servers: null
            }
            put: null
            post: null
            delete: null
            options: null
            head: null
            patch: null
            trace: null
            servers: null
            parameters: null
            $ref: null
        }}
    }
    components: class Components {
        schemas: {Inline_response_map200=class ObjectSchema {
            class Schema {
                type: object
                format: null
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: {id=class IntegerSchema {
                    class Schema {
                        type: integer
                        format: null
                        $ref: null
                        description: null
                        title: null
                        multipleOf: null
                        maximum: null
                        exclusiveMaximum: null
                        minimum: null
                        exclusiveMinimum: null
                        maxLength: null
                        minLength: null
                        pattern: null
                        maxItems: null
                        minItems: null
                        uniqueItems: null
                        maxProperties: null
                        minProperties: null
                        required: null
                        not: null
                        properties: null
                        additionalProperties: null
                        nullable: null
                        readOnly: null
                        writeOnly: null
                        example: null
                        externalDocs: null
                        deprecated: null
                        discriminator: null
                        xml: null
                    }
                }, age=class IntegerSchema {
                    class Schema {
                        type: integer
                        format: null
                        $ref: null
                        description: null
                        title: null
                        multipleOf: null
                        maximum: null
                        exclusiveMaximum: null
                        minimum: null
                        exclusiveMinimum: null
                        maxLength: null
                        minLength: null
                        pattern: null
                        maxItems: null
                        minItems: null
                        uniqueItems: null
                        maxProperties: null
                        minProperties: null
                        required: null
                        not: null
                        properties: null
                        additionalProperties: null
                        nullable: null
                        readOnly: null
                        writeOnly: null
                        example: null
                        externalDocs: null
                        deprecated: null
                        discriminator: null
                        xml: null
                    }
                }}
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }, Inline_response_200=class MapSchema {
            class Schema {
                type: object
                format: null
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: {name=class StringSchema {
                    class Schema {
                        type: string
                        format: null
                        $ref: null
                        description: null
                        title: null
                        multipleOf: null
                        maximum: null
                        exclusiveMaximum: null
                        minimum: null
                        exclusiveMinimum: null
                        maxLength: null
                        minLength: null
                        pattern: null
                        maxItems: null
                        minItems: null
                        uniqueItems: null
                        maxProperties: null
                        minProperties: null
                        required: null
                        not: null
                        properties: null
                        additionalProperties: null
                        nullable: null
                        readOnly: null
                        writeOnly: null
                        example: null
                        externalDocs: null
                        deprecated: null
                        discriminator: null
                        xml: null
                    }
                }, age=class IntegerSchema {
                    class Schema {
                        type: integer
                        format: null
                        $ref: null
                        description: null
                        title: null
                        multipleOf: null
                        maximum: null
                        exclusiveMaximum: null
                        minimum: null
                        exclusiveMinimum: null
                        maxLength: null
                        minLength: null
                        pattern: null
                        maxItems: null
                        minItems: null
                        uniqueItems: null
                        maxProperties: null
                        minProperties: null
                        required: null
                        not: null
                        properties: null
                        additionalProperties: null
                        nullable: null
                        readOnly: null
                        writeOnly: null
                        example: null
                        externalDocs: null
                        deprecated: null
                        discriminator: null
                        xml: null
                    }
                }}
                additionalProperties: class IntegerSchema {
                    class Schema {
                        type: integer
                        format: int32
                        $ref: null
                        description: null
                        title: null
                        multipleOf: null
                        maximum: null
                        exclusiveMaximum: null
                        minimum: null
                        exclusiveMinimum: null
                        maxLength: null
                        minLength: null
                        pattern: null
                        maxItems: null
                        minItems: null
                        uniqueItems: null
                        maxProperties: null
                        minProperties: null
                        required: null
                        not: null
                        properties: null
                        additionalProperties: null
                        nullable: null
                        readOnly: null
                        writeOnly: null
                        example: null
                        externalDocs: null
                        deprecated: null
                        discriminator: null
                        xml: null
                    }
                }
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }}
        responses: null
        parameters: null
        examples: null
        requestBodies: null
        headers: null
        securitySchemes: null
        links: null
        callbacks: null
    }
}