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

Compilation error when using 'allOf' in schema #811

Open
rmanibus opened this issue Sep 27, 2024 · 2 comments
Open

Compilation error when using 'allOf' in schema #811

rmanibus opened this issue Sep 27, 2024 · 2 comments
Labels
area:client This item is related to the client extension

Comments

@rmanibus
Copy link
Contributor

The following open api spec is generating code that does not compile.

openapi: 3.0.3
info:
  title: A Test
  version: 1.0.0

paths:
    /factors:
        get:
          summary: Get Factors
          responses:
            '200':
              description: Factors
              content:
                application/json:
                  schema:
                    $ref: '#/components/schemas/UserFactor'
components:
  schemas:
    UserFactorType:
      description: Type of Factor
      type: string
      enum:
        - call
        - web
    UserFactor:
      type: object
      properties:
        factorType:
          $ref: '#/components/schemas/UserFactorType'
        id:
          description: ID of the Factor
          type: string
          example: caf8m6jbcvUH8mAep1d7
          readOnly: true
        profile:
          type: object
          description: Specific attributes related to the Factor
      discriminator:
        propertyName: factorType
        mapping:
          call: '#/components/schemas/UserFactorCall'
          web: '#/components/schemas/UserFactorWeb'
    UserFactorCall:
      title: call
      allOf:
        - $ref: '#/components/schemas/UserFactor'
        - type: object
          properties:
            factorType:
              example: call
            provider:
              enum:
                - AAA
    UserFactorWeb:
      title: web
      allOf:
        - $ref: '#/components/schemas/UserFactor'
        - type: object
          properties:
            factorType:
              example: web
            provider:
              enum:
                - BBB

here is the generated parent class:

public class UserFactor  {

    private UserFactorType factorType;

    [...]
    
    /**
    * Get factorType
    * @return factorType
    **/
    @JsonProperty("factorType")
    @com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
    public UserFactorType getFactorType() {
        return factorType;
    }

and here is the child class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class UserFactorWeb extends UserFactor {

    // This should be of type UserFactorType
    private Object factorType = null;

    /**
    * Get factorType
    * @return factorType
    **/
    @JsonProperty("factorType")
    @com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
     // This should  return UserFactorType
    public Object getFactorType() {
        return factorType;
    }

    /**
     * Set factorType
     **/
    public void setFactorType(Object factorType) {
        this.factorType = factorType;
    }

    public UserFactorWeb factorType(Object factorType) {
        this.factorType = factorType;
        return this;
    }
  }
@rmanibus rmanibus changed the title Compilation error when using 'allOf' Compilation error when using 'allOf' in schema Sep 27, 2024
@ricardozanini
Copy link
Member

Can you send a PR to fix it?

@ricardozanini ricardozanini added bug Something isn't working area:client This item is related to the client extension labels Oct 3, 2024
@rmanibus
Copy link
Contributor Author

rmanibus commented Oct 7, 2024

looking into this a little bit more, the problem comes from the fact that the property factorType is first defined in the UserFactor, and then the example for this same field is defined directly in the child type UserFactorCall. The current implementation does not merge both definitions and the second field ends up being handled as its own independent field with type Object since nothing else is given at this level.

I think that the author wanted to leverage some kind of inheritance, where the field is generally defined in the parent type and then refined (in this case adding an example or a comment) in the child type after the allOff.

I am not sure if this is a valid pattern or not, but it comes from the official okta spec.

The only thing that I found is this doc:
https://redocly.com/docs/resources/all-of#type-override-is-invalid
Which says

Overriding a description and summary is allowed. From an evaluation perspective, it works because the description is going to match any type.

My gut feeling is that if this is a valid pattern, the issue is more in open-api gen than in this project.

@ricardozanini ricardozanini removed the bug Something isn't working label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:client This item is related to the client extension
Projects
None yet
Development

No branches or pull requests

2 participants