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

resolveFully together with allOf looses required properties #2081

Closed
david0 opened this issue Apr 26, 2024 · 1 comment
Closed

resolveFully together with allOf looses required properties #2081

david0 opened this issue Apr 26, 2024 · 1 comment
Assignees

Comments

@david0
Copy link

david0 commented Apr 26, 2024

We are trying to enforce some properties via required together with allOf to reuse existing definitions:

openapi: 3.0.0
info:
  title: Minimal OpenAPI 3.0 with required on the same allOf
  version: 1.0.0
paths:
  /pets:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PetCreate'
      responses:
        '201':
          description: Created
components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string

    PetCreate:
      allOf:
        - $ref: '#/components/schemas/Pet'
      required:
        - name

But using setResolveFully(true), the required attribute will get lost:

    @Test
    public void requiredAllResolveFully() {
        ParseOptions options = new ParseOptions();
        options.setResolveFully(true);
        OpenAPI openAPI = new OpenAPIV3Parser().read("required-anyOf.yaml", null, options);

        RequestBody requestBody = openAPI.getPaths().get("/pets").getPost().getRequestBody();
        Schema requestBodySchema = requestBody.getContent().get("application/json").getSchema();
        assertNull(requestBodySchema.get$ref()); // has been inlined

        Schema schema = requestBody.getContent().get("application/json").getSchema();
        assertEquals(schema.getProperties().size(),2);
        assertEquals(schema.getRequired().size(),1); // <-- FAILS, as its empty
    }
@david0
Copy link
Author

david0 commented Apr 26, 2024

Another variation of this bug with nested allOf/anyOf:

openapi: 3.0.0
info:
  title: Minimal OpenAPI 3.0 with allOf
  version: 1.0.0
paths:
  /pets:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PetCreate'
      responses:
        '201':
          description: Created
components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
    Cow:
      type: object
    PetCreate:
      allOf:  
        - anyOf:
          - $ref: '#/components/schemas/Pet'
          - $ref: '#/components/schemas/Cow'
        - properties: 
            name:
              type: string
  @Test
    public void allOfAnyOfResolveFully() {
        ParseOptions options = new ParseOptions();
        options.setResolveFully(true);
        OpenAPI openAPI = new OpenAPIV3Parser().read("required-anyOf.yaml", null, options);

        RequestBody requestBody = openAPI.getPaths().get("/pets").getPost().getRequestBody();
        Schema requestBodySchema = requestBody.getContent().get("application/json").getSchema();
        assertNull(requestBodySchema.get$ref()); // has been inlined

        Schema schema = requestBody.getContent().get("application/json").getSchema();
        assertEquals(schema.getProperties().size(),2);  // <!--FAILS, since id got lost
    }

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

No branches or pull requests

2 participants