Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][JAVA] Bug generating spring server: codegenModel is null. Default to UNKNOWN_BASE_TYPE #2236

Closed
5 of 6 tasks
fjs018 opened this issue Feb 25, 2019 · 22 comments
Closed
5 of 6 tasks

Comments

@fjs018
Copy link

fjs018 commented Feb 25, 2019

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

I am trying to generate a java spring server using 4.0.0-beta2, however generation fails with:

[main] WARN  o.o.codegen.DefaultCodegen - codegenModel is null. Default to UNKNOWN_BASE_TYPE
[main] WARN  o.o.codegen.DefaultCodegen - The following schema has undefined (null) baseType. It could be due to form parameter defined in OpenAPI v2 spec with incorrect consumes. A correct 'consumes' for form parameters should be 'application/x-www-form-urlencoded' or 'multipart/form-data'
[main] WARN  o.o.codegen.DefaultCodegen - schema: class ComposedSchema {
    class Schema {
        title: null
        multipleOf: null

The .java with the generated APIs has the following:
  import org.openapitools.model.UNKNOWN_BASE_TYPE;
  The default ResponseEntity has:
ApiParam(value = "Click on the **Model** below to view the available parameters for this endpoint." ,required=true )  @Valid @RequestBody UNKNOWN_BASE_TYPE UNKNOWN_BASE_TYPE) {
openapi-generator version

4.0.0-beta2 and previous versions.

OpenAPI declaration file content or url
        description: >-
          Click on the **Model** below to view the available parameters for this
          endpoint.
        required: true
        content:
          application/json:
            schema:
              allOf:
                - type: object
                  properties:
                    key:
                      type: string
                      example: S1111...111111
                    alg:
                      type: string
                      example: abc
                    params:
                      type: object
                      oneOf:
                        - $ref: '#/components/schemas/abc_params'
                        - $ref: '#/components/schemas/abb_params'
                        - $ref: '#/components/schemas/aaa_params'
                      example:
                        num: '5521204534320004'
                        expiryDate: 0519
                        sCode: '123'
                      discriminator:
                        propertyName: alg
                        mapping:
                          abc: '#/components/schemas/abc_params'
                          abb: '#/components/schemas/abb_params'
                          aaa: '#/components/schemas/aaa_params'```




##### Command line used for generation

java -jar openapi-generator-cli.jar generate  -i myAPI.yaml -g spring -o spring_server -c  config.json


##### Steps to reproduce

<!-- unambiguous set of steps to reproduce the bug.-->

##### Related issues/PRs

<!-- has a similar issue/PR been reported/opened before? Please do a search in https://github.com/openapitools/openapi-generator/issues?utf8=%E2%9C%93&q=is%3Aissue%20 -->

##### Suggest a fix

<!-- if you can't fix the bug yourself, perhaps you can point to what might be
  causing the problem (line of code or commit), or simply make a suggestion -->
@auto-labeler
Copy link

auto-labeler bot commented Feb 25, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@fjs018
Copy link
Author

fjs018 commented Feb 26, 2019

I believe that this is all related to the discussion in: #15
@jmini , can you confirm?

@hugotavares
Copy link

Hi,
I'm facing this issue as well. Still, I'd like to know whether it exists a workaround or not for it, even though it is by manually setting any configuration parameter on the pom.xml which generates the API.
Thanks in advance.

@Emdee89
Copy link

Emdee89 commented Apr 30, 2019

Facing the same issue. Can confirm that the issues does occur once you use oneOf like so:

  /demonstrations:
    post:
      summary: Create Demonstration
      tags: 
        - Demonstrations
      operationId: createDemonstration
      requestBody:
        description: Demonstration details.
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/demonstrationPayload2'
                - $ref: '#/components/schemas/demonstrationPayload1'
      responses:
        '201':
          description:  created

When we comment oneOf and only mention one entry of $ref, then it works.

@wing328
Copy link
Member

wing328 commented Apr 30, 2019

As a workaround, what about defining the schema separately (instead of inline)? e.g. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/allOf.yaml#L43-L52

@Emdee89
Copy link

Emdee89 commented Apr 30, 2019

@wing328: I tried that, then it parses the yaml correctly and the source code compiles. However, when you look at the generated code, it does not do what the semantics would imply.

In my experiments, the code generator generates a Java class that looks exactly (of course, except its name) like the Java class that corresponds to the schema mentioned at last under the oneOf declaration. What I'd like to have instead is a Java class that is a super-type of the two mentioned schemas.

@wing328
Copy link
Member

wing328 commented Apr 30, 2019

I remember the oneOf implementation in Java is still being discussed/worked on.

What I'd like to have instead is a Java class that is a super-type of the two mentioned schemas.

If it's a super-type, what about using allOf instead?

@alfirin
Copy link

alfirin commented Sep 4, 2019

Same issue here and I don't wat to use the allOf instead.

@dkirrane
Copy link

related to
#2845

@jandriescher
Copy link

Hey everyone. Was there ever a solution found for this? Facing the same issue with oneOf currently.

@tejarao25
Copy link

Facing the same issue with oneOf. Any solution for this issue?

@iekdosha
Copy link

I still have this issue with Spring generator. Anything new?

@rossmills3
Copy link

Generating code that implements oneOf and allOf requires the use of inheritance in the model. From what I see, the generated code does not use inheritance. However, if I use SwaggerHub site to generate code, inheritance is used to implement oneOf and allOf.

@russ-shmus
Copy link

did anyone ever get to the bottom if it? I still see that even latest maven plugin 5.0.0-beta2 fails to generate inheritance code, whereas online generator processes exactly the same yaml correctly. Tried all workarounds found, no luck..

@skchande
Copy link

skchande commented Sep 29, 2020

Does "openapi-generator validate -i filename.yml" give you any tips?

Have you tried using Version 3.0?

@frochi42
Copy link

Hi,
any progress or workaround here? We are stuck at the same problem, even with version 5.1.1 of theopenapi-generator-maven-plugin.
Thanks a lot!

@Bubo-DBM
Copy link

Bubo-DBM commented May 13, 2021

@wing328 Hi, same as @frochi42 I'm getting UNKNOWN_BASE_TYPE with version 5.1.1 with openapi-generator-cli for angular-typescript. Using https://editor.swagger.io/ I'm getting the correct client code.

Below is example yaml code:
`openapi: 3.0.1
info:
title: 'Test Client'
version: 'v1'
description: |
This component store resources and authorizations to MongoDB

paths:
/api/v1/resources/apply-configuration:
post:
operationId: apply-resource
tags:
- 'resources'
summary: apply resource
parameters:
- in: header
name: X-Security
schema:
$ref: '#/components/schemas/header.x-security'
required: true
requestBody:
content:
application/json:
schema:
required: [configuration]
properties:
configuration:
required: [name, type]
properties:
name:
$ref: '#/components/schemas/value.string'
type:
allOf:
- $ref: '#/components/schemas/value.string'
- enum: [security.template/account.v1, security.template/company.v1]
example: security.template/account.v1
description: |
Field | Description
-|-
name | string - the unique name of the configuration
type | string - the configuration type
description: |
Field | Description
-|-
configuration | object - contain configuration propeties
responses:
202:
$ref: '#/components/responses/200-empty'
4XX:
$ref: '#/components/responses/4XX'
5XX:
$ref: '#/components/responses/5XX'
503:
$ref: '#/components/responses/503'

components:
responses:
200-empty:
description: 'Empty success answer'
4XX:
content:
application/json:
schema:
type: object
properties:
code:
type: string
example: 'VAIDATION_ERROR'
correlation:
type: string
example: '146a99c0b0aced9a'
errors:
type: array
items:
type: object
properties:
field:
type: string
example: 'field1'
message:
type: string
example: 'can not be null'
description: 'Client error'
5XX:
content:
application/json:
schema:
$ref: '#/components/schemas/value.error'
description: 'Server error'
503:
content:
application/json:
schema:
$ref: '#/components/schemas/value.error'
description: 'Service unavailable'

schemas:
value.string:
type: string
example: 'any'
description: String value
value.error:
properties:
error:
properties:
code:
type: string
example: 'ERROR'
correlation:
type: string
example: 'any'
description: Error
header.x-security:
type: string
example: '%7B%20%22user%22%3A%20%7B%20%22name%22%3A%20%22cf678c35-806a-47da-b92d-0144e23eb3be%22%2C%20%22alias%22%3A%20%22Lorenzo%20Dodi%22%20%7D%20%7D'
description: 'JSON URL encoded which contain user informatinos as "name" and "alias"'

`

@trackq
Copy link

trackq commented Aug 17, 2021

fyi. this also happens for python-fastapi server generation

@router.post(
    "/callthrough",
    responses={
        201: {"model": Call, "description": "Example response"},
    },
    tags=["default"],
    summary="Create a callthrough",
)
async def post_callthrough(
    unknown_base_type: UNKNOWN_BASE_TYPE = Body(None, description=""),
) -> Call:
    """Createa a new callthrough Call"""
    ...

@falnyr falnyr mentioned this issue Sep 1, 2021
5 tasks
@ghost
Copy link

ghost commented Jan 31, 2022

Tried the following in FastApi, but changing to allOf still fails with OpenApi Generator 😢:

def custom_openapi() -> dict[str, dict]:
    openapi_schema = get_openapi(
        title=constants.PROJECT_NAME,
        description=constants.DESCRIPTION,
        version=constants.VERSION,
        routes=app.routes,
    )

    paths = openapi_schema["paths"]
    for path in paths.values():
        for method in path.values():
            if (body := "requestBody") in method:
                schema = method[body]["content"]["application/json"]["schema"]
                if "anyOf" in schema.keys():
                    schema["allOf"] = schema.pop("anyOf")

    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

@dbcall2
Copy link

dbcall2 commented Mar 2, 2022

This issue was opened three years ago!! The spring code generator is STILL completely broken on polymorphic request/respond bodies. What's causing the delay? Do you guys need people to work on this? Is it internal squabbling about how to "fix" it? I'll gladly volunteer some time to get this fixed as this tech holds promise.

Update: This is not an easy problem to solve... lol. The schema model handling in the spring generator isn't smart enough to traverse the object hierarchy to determine a suitable common parent. And since this is done "pre-java", there's no way to resolve the base class through reflection because it doesn't exist yet. I'm currently looking at ways to solve these issues.

@CoenWarmer
Copy link

Any update @dbcall2?

@wing328
Copy link
Member

wing328 commented May 13, 2022

I've merged #12353 to the master which seems to address the inline schema issues in the request bodies. Please pull the latest master to give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests