3

The Problem

With version 6.x of Open API Generator using ASP.NET Core, nested object types are being created as their own classes for reasons unknown. 5.x versions of the openapi-generator used the same class for nested objects.

With these two components within a yml specification (event-api-v1.swagger.3.0.3.yaml from example repo)

components:
  schemas:
    EventIdentifier:
      title: EventIdentifier
      type: object
      properties:
        eventId:
          type: string
        occurrenceDate:
          type: string
          nullable: true
      example:
        eventId: '43128940213498123'
        occurrenceDate: '2021-05-30T15:00:00+12:00'

    BookingCreate:
      title: BookingCreate
      type: object
      description: A particular person's booking for an event.
      required:
        - eventId
        - personId
      properties:
        eventId:
          allOf:
            - $ref: '#/components/schemas/EventIdentifier'
          description: 'A unique identifier for the event that the booking relates to.'
        personId:
          type: string
          nullable: false

Versions 5.x generates C# as

public class BookingCreate : IEquatable<BookingCreate>
{
    public EventIdentifier EventId { get; set; }
}

however, versions 6.x are generating as

public class BookingCreate : IEquatable<BookingCreate>
{
    public BookingCreateEventId EventId { get; set; }
}

This is a huge blocker for adopting 6.x versions of the openapi-generator for an established large project.

Question

Can this change in behavior be reverted to the 5.x behavior with configuration or with a custom template?

Regarding a custom template, on inspecting the model.mustache file, the provided value for dataType is being supplied as BookingCreateEventId/SpeakerCreateEventId for the EventId property and so this cannot be readily customised.

Observations

Changing the openapi spec to 3.1 (example, event-api-v1.swagger.3.1.yaml) results in the type of the EventId being set back to EventIdentifier but the generator does not yet support 3.1 and so all of the primitive types are generated as object.

Code

A small testing project can be found here

Attempts

Have attempted to use versions 6.0, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6 of openapi-generator but have been unable to replicate code output from 5.x versions

Marc Stevenson
  • 1,090
  • 8
  • 9

1 Answers1

2

I can confirm I got the same behaviour and it is not only on the aspnetcore generator.

Upon further inspection I believe the issue you are facing is due to you using the allOf operator (which I think confuse the parser if the object mentioned is a combined object or not?).

Remove it and the correct Model should be generated.

(https://github.com/marcsstevenson/OpenApiGeneratorTesting/pull/1)

Relevant GitHub issue https://github.com/OpenAPITools/openapi-generator/issues/12838

qkhanhpro
  • 4,371
  • 2
  • 33
  • 45
  • Interestingly, remove all the "description" result in a single BookingCreateEventId being created and both BookingCreate + SpeakerCreate refer to that same BookingCreateEventId. This area of the generator seems to be really bugged... – qkhanhpro Jun 23 '23 at 07:55