OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.24k stars 6.43k forks source link

[BUG][JAVA] schemaMapping not working for POST requestBody #13987

Open sh4nks opened 1 year ago

sh4nks commented 1 year ago

Hey!

Bug Report Checklist

Description

I can't get the schemaMapping to work with a POST request where I reference a object from an external package via schemaMapping. It works flawlessly for returning objects though. See my provided examples.

openapi-generator version

6.2.1

OpenAPI declaration file content or url
paths:
  /test/loadTest:
    get:
      tags:
        - test
      operationId: loadTest
      responses:
        200:
          description: Test object
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Test'

  /test/saveTest:
    post:
      tags:
        - test
      operationId: saveTest
      requestBody:
        description: TestToSave object
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TestToSave'
      responses:
        201:
          description: Created

components:
  schema:
    Test:
      type: object
      description: "external Test object"

    TestToSave:
      type: object
      description: "external TestToSave object"
Generation Details

My schemaMappings maven configuration:

<schemaMappings>
    Test=my.external.test.package.model.Test,
    TestToSave=my.external.test.package.model.TestToSave,
</schemaMappings>

As you can see the saveTest(@NotNull Object body) method isn't correctly mapped to my.external.test.package.api.model.TestToSave, even though I have set it in the schema mappings. The weird thing is, that the schema mapping works correctly with the loadTest() method.

@ServiceInterface
@Local
public interface TestService {

    /**
     * @return my.test.package.api.model.Test
     */
    my.external.test.package.api.model.Test loadTest() throws TestUiException;

    /**
     * @param body TestToSave object
     */
    void saveTest(@NotNull Object body) throws TestUiException; // <-- should be my.external.test.package.api.model.TestToSave
}

Thanks!

borsch commented 1 year ago

@sh4nks could you please provide complete plugin configuration from pom.xml, that you are using?

sh4nks commented 1 year ago

@sh4nks could you please provide complete plugin configuration from pom.xml, that you are using?

Sure:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>

    <configuration>
        <schemaMappings>
            Test=my.external.test.package.model.Test,
            TestToSave=my.external.test.package.model.TestToSave
        </schemaMappings>

        <verbose>false</verbose>
        <configOptions>
            <providedIn>root</providedIn>
            <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
            <enforceGenericModuleWithProviders>true</enforceGenericModuleWithProviders>
        </configOptions>
    </configuration>
</plugin>

btw, the same problem exists when using the typescript-angular generator - the GET methods are being generated just fine and the POST methods body has the any type instead of the TestToSave type.

sh4nks commented 1 year ago

when adding a property it kinda works but doesn't feel right:

    TestToSave:
      description: "external TestToSave object"
      properties:
        none:
          type: string
    void saveTest(@NotNull @Valid my.external.test.package.model.TestToSave myExternalTestPackageModelTestToSave) throws TestUiException;
sh4nks commented 1 year ago

I am experiencing a similar issue when using the typescript-angular generator. For example, in my model definition I have the following two components:

components:
  schema:

    CombinedTest:
      description: "combined Test + TestToSave"
      type: object
      properties:
        belongsHere:
          type: string
        tests:
          type: array
          items:
            $ref: "#/components/schemas/Test"
        testsToSave:
          type: array
          items:
            $ref: "#/components/schemas/TestToSave"

    Test:
      description: "external Test object"

    TestToSave:
      description: "external TestToSave object"
      properties:
        dummyproperty:
          type: string

will be generated into:

import { Test } from '@my/test_package/lib/gen/model/test';
import { TestToSave } from '@my/test_package/lib/gen/model/testToSave';

export interface Zusammenfassung { 
    belongsHere?: string;
    tests?: Array<any>;  // should be Array<Test>
    testsToSave?: Array<TestToSave>;  // is OK
}

the maven configuration:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <configuration>
        <schemaMappings>
            Test=@my/test_package/lib/gen/model/test,
            TestToSave=@my/test_package/lib/gen/model/testToSave
        </schemaMappings>
        <importMappings>
            Test=@my/test_package/lib/gen/model/test,
            TestToSave=@my/test_package/lib/gen/model/testToSave
        </importMappings>
        <verbose>true</verbose>
        <configOptions>
            <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
        </configOptions>
    </configuration>
</plugin>
vicmosin commented 1 year ago

Getting the same issue while trying to define the requestBody schema as array. Any advises to workaround the behaviour?

sh4nks commented 1 year ago

@vicmosin I have worked around the issue by adding a "dummy" property:

    TestToSave:
      description: "external TestToSave object"
      properties:
        dummyproperty: # <-- this here
          type: string

After adding this, the schema is correctly mapped...

ghilainm commented 6 months ago

Same issue here with Spring WebFlux generator.

I have a mapping from MyType to MyExternalType

I have a simple type like this

    MyType:
      type: string
      description: My type
      maxLength: 200
      pattern: |-
        [A-zÀ-ú0-9_\-/:%'@]*

I am mapping it to a custom external class but the generator generates Mono instead of Mono.

However, when this type is used somewhere else than in request body, it works perfectly.