OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Spring] allOf conversion issue

Open
#21,275 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • [] 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

When a schema is extended, using allOf, and some constraints are applied to the properties of the original object, but the constraints are inserted above the reference to the original schema; the constraints are not applied in the generated code. Whereas, the swagger UI seems to render the schemas correctly.

openapi-generator version

version "7.12.0"

OpenAPI declaration file content or url

# Base Schema

    PlantOfferEditRequest:
      type: object
      properties:
        name:
          type: string
        imageURL:
          type: string
        averageDaysToHarvest:
          type: integer
        price:
          $ref: '#/components/schemas/Price'

# Not Working Schema

PlantOfferCreateRequest:
      allOf:
        - type: object
          properties:
            gardenId:
              type: integer
              format: int64
          required:
            - gardenId
            - name
            - imageURL
            - averageDaysToHarvest
            - price
        - $ref: '#/components/schemas/PlantOfferEditRequest'

# Working Schema

PlantOfferCreateRequest:
      allOf:
        - type: object
          properties:
            gardenId:
              type: integer
              format: int64
          required:
            - gardenId
        - $ref: '#/components/schemas/PlantOfferEditRequest'
        - type: object
          required:
            - name
            - imageURL
            - averageDaysToHarvest
            - price

Expected vs Actual Output

Expected:

public class PlantOfferCreateRequest {

private String name;

private String imageURL;

private Integer averageDaysToHarvest;

private Price price;

private Long gardenId;
}

Actual:

public class PlantOfferCreateRequest {

private @Nullable String name;

private @Nullable String imageURL;

private @Nullable Integer averageDaysToHarvest;

private @Nullable Price price;

private Long gardenId;
}

Generation Details

The gradle plugin configuration:

openApiGenerate {
    generatorName.set("spring")
    inputSpec.set("$projectDir/src/main/resources/openapi.yaml")
    outputDir.set("$buildDir/generated")
    apiPackage.set("com.myname.prjname.api")
    modelPackage.set("com.myname.prjname.model")
    configOptions.set(
        mapOf(
            "library" to "spring-boot",
            "useSpringBoot3" to "true",
            "language" to "Kotlin",
            "interfaceOnly" to "true",
            "useTags" to "true",
        	"useOperationId" to "true"
        )
    )
	typeMappings.set(
		mapOf(
			"Page" to "org.springframework.data.domain.Page",
			"Pageable" to "org.springframework.data.domain.Pageable",
			"Sort" to "org.springframework.data.domain.Sort",
			"PageGardenInfo" to "org.springframework.data.domain.Page<com.myname.prjname.model.GardenInfo>",
			"PagePlantOffer" to "org.springframework.data.domain.Page<com.myname.prjname.model.PlantOffer>",
			"PagePlantedPlant" to "org.springframework.data.domain.Page<com.myname.prjname.model.PlantedPlant>",
			"PageGardener" to "org.springframework.data.domain.Page<com.myname.prjname.model.Gardener>"
		)
	)
}
Steps to reproduce

1 - Create a schema with some non-required properties.
2 - Create another schema, using the allOf construct, that extends the first one.
3 - Add some constraints to the properties from the previous schema, listing above the reference to the previous schema.

Suggest a fix

Instead of iterating over the schemas and applying the constraints as they come, store them in a secondary location and apply them all at the end.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied PlantOfferCreateRequest and PlantOfferEditRequest schemas with the Spring generator, comparing the generated nullable annotations with the expected output. Trace the Spring generator's allOf model handling and verify that required constraints remain effective regardless of their position relative to the schema reference; done means the reproduced model has the expected required properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, openapi, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.