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][SPRINGBOOT] The class is generated without extends parent #20521

Open
4 of 6 tasks
p-migda opened this issue Jan 22, 2025 · 1 comment
Open
4 of 6 tasks

[BUG][SPRINGBOOT] The class is generated without extends parent #20521

p-migda opened this issue Jan 22, 2025 · 1 comment

Comments

@p-migda
Copy link

p-migda commented Jan 22, 2025

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I want to generate a class inheritated from imported external class.
In gradle task im using importMappings to import generated class from another yaml file.
In yaml file im using allOf and $ref to that class.
Class will be generated with import to that class but without of extends keyword.

openapi-generator version

7.11.0

OpenAPI declaration file content or url

https://gist.github.com/p-migda/c32e0e2623024a00cd068343cc6c25d2
https://gist.github.com/p-migda/f9c211cfb5849cd3cc32841b3d0f63b4

Generation Details

using openapi-generator-gradle-plugin:

tasks.register('generateParent', GenerateTask) {
    skipOperationExample = true
    generatorName = 'spring'
    inputSpec = "$projectDir/exampleParent.yaml"
    outputDir = "$projectDir"
    apiPackage = 'org.example.document.generated.api'
    modelPackage = 'org.example.document.generated.model'
    configOptions = [
            'useSpringBoot3'                : 'true',
            'interfaceOnly'                 : 'true',
            'serializationLibrary'          : 'jackson',
            'dateLibrary'                   : 'java8',
            'library'                       : 'spring-boot',
            'sourceFolder'                  : targetFolder,
            'bigDecimalAsString'            : 'false',
            'additionalModelTypeAnnotations': ';@com.fasterxml.jackson.annotation.JsonInclude(' +
                    'com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL);' +
                    '@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true);' +
                    '@lombok.NoArgsConstructor;',
    ]
    additionalProperties = [
            generatedConstructorWithRequiredArgs: 'false'
    ]
}

tasks.register('generateChild', GenerateTask) {
    dependsOn generateParent
    skipOperationExample = true
    generatorName = 'spring'
    inputSpec = "$projectDir/example.yaml"
    outputDir = "$projectDir"
    apiPackage = 'org.example.document.generated.api'
    modelPackage = 'org.example.document.generated.model'
    configOptions = [
            'useSpringBoot3'                : 'true',
            'interfaceOnly'                 : 'true',
            'serializationLibrary'          : 'jackson',
            'dateLibrary'                   : 'java8',
            'library'                       : 'spring-boot',
            'sourceFolder'                  : targetFolder,
            'bigDecimalAsString'            : 'false',
            'additionalModelTypeAnnotations': ';@com.fasterxml.jackson.annotation.JsonInclude(' +
                    'com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL);' +
                    '@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true);' +
                    '@lombok.NoArgsConstructor;',
    ]
    additionalProperties = [
            generatedConstructorWithRequiredArgs: 'false'
    ]
    importMappings = [
            'ParentClass': 'org.example.document.generated.model.ParentClass'
    ]
}
Steps to reproduce

Just run a gradleTask

Suggest a fix

Maybe something wrong is with setting a parent marker when parent class is imported from outside of file.

@jpfinne
Copy link
Contributor

jpfinne commented Jan 22, 2025

First you need to configure ParentClass.

ParentClass:
     x-parent: true
     type: object
     properties:
        parentTestField:
          type: boolean

Parent class must use x-parent or a discriminator to force inheritance instead of composition. Alternatively REF_AS_PARENT_IN_ALLOF normalization can be set.

The generator uses openapi contracts to analyze the structure and the hierarchy of the models.
There is no way to perform that with imported class.

There are several workarounds.

You can use a reference to an extern file or url.

ChildClass:
  allOff:
  - $ref: './ParentExample.yaml#/components/schemas/ParentClass' 
  - properties:
    test:
      type: string

If ParentClass is hand written in java, you can still make it work with some additional work.

Embedd a dummy ParentClass in your contract

ParentClass:
   x-parent: true
   type: object

Configure the generator with schemaMappings, importMapping, typeMappings.
'ParentClass': 'org.example.document.generated.model.ParentClass'
.openapi-generator-ignore could also contains **/ParentClass.java

With this config, the generator will skip the generation of ParentClass.java.
Add the jar containing it as a dependency so the compiler finds it

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

2 participants