Skip to content

AllOf does not respect required for referenced properties #604

Closed

Description

@Swiftwork

Great work on the new allOf rewrite. It generates nearly identical types to the workaround I mentioned here #531 (comment). However, it does not seem to respect the required section for referenced properties. Using the below partial schema:

schemas:
  id:
    type: string
    title: id
    description: Unique identifier for the given resource.
    pattern: '^[a-zA-Z0-9]+$'
    example: 1qyOUSitRuN5QPSFfVg6AuiUuzF
    x-go-type: ksuid.KSUID
    minLength: 27
    maxLength: 27
    readOnly: true
  User:
    title: User
    allOf:
      - type: object
        properties:
          id:
            $ref: '#/components/schemas/id'
        required:
          - id
      - $ref: '#/components/schemas/UserProperties'
      - type: object
        required:
          - email
          - name
          - photo
          - verified

We can see it generates the following type:

// User defines model for User.
type User struct {
  Email openapi_types.Email `json:"email"`
  
  // Unique identifier for the given resource.
  Id *Id `json:"id,omitempty"`
  
  // Contains the full name.
  Name string `json:"name"`
  
  // Link to the gravatar profile photo.
  Photo    string `json:"photo"`
  Verified bool   `json:"verified"`
}

Where Email, Name, Photo, and Verified are required but Id is not. Maybe there is some configuration I am missing or this use-case hasn't been taken into account.

Activity

  1. florentchauveau commented on Jun 2, 2022

    @florentchauveau
    Contributor

    I was about to post the exact same issue.

    That's because your "id" property has "readOnly: true". Readonly properties are generated with pointers.

    I am submitting a pull request right now.

  2. added a commit that references this issue on Jun 2, 2022
    9a7b81f
  3. Swiftwork commented on Jun 2, 2022

    @Swiftwork
    Author

    I was about to post the exact same issue.

    That's because your "id" property has "readOnly: true". Readonly properties are generated with pointers.

    I am submitting a pull request right now.

    Haha perfect, I can confirm that removing readOnly removes the pointer issue.

  4. florentchauveau commented on Jun 3, 2022

    @florentchauveau
    Contributor

    Afterthoughts:

    In this case we are talking about server side code. A readOnly property that is required should not be a pointer (because it has to be returned by the server).

    But what about client side? I think in that case it made sense to have a pointer, because the client should not send the property anyway.

    Should we split the objects and have different definitions between client and server?

  5. florentchauveau commented on Jun 3, 2022

    @florentchauveau
    Contributor

    Here are the relevant parts I found in the specifications:

    3.0.3: https://github.com/OAI/OpenAPI-Specification/blob/54edcf039c9854dcbc4feac1b1990c21972f3ca5/versions/3.0.3.md#properties

    Relevant only for Schema "properties" definitions. Declares the property as "read only". This means that it MAY be sent as part of a response but SHOULD NOT be sent as part of the request. If the property is marked as readOnly being true and is in the required list, the required will take effect on the response only. A property MUST NOT be marked as both readOnly and writeOnly being true.

    However in the 3.1.0 spec, the property definitions follow those of JSON Schema and do not add any additional semantics.

    https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.9.4

    If "readOnly" has a value of boolean true, it indicates that the value of the instance is managed exclusively by the owning authority, and attempts by an application to modify the value of this property are expected to be ignored or rejected by that owning authority.

    https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.5.3

    An object instance is valid against this keyword if every item in the array is the name of a property in the instance.

    No word on mixing required and readOnly.

  6. added 3 commits that reference this issue on Jun 3, 2022
    09fce3b
    058bbfb
    5f2116a
  7. florentchauveau commented on Jun 3, 2022

    @florentchauveau
    Contributor

    I have updated my pull request #605 to create a new configuration in the compatibility section.

    compatibility:
      disable-required-readonly-as-pointer: true

    This way, client code can be generated with the default settings (pointers), but the server can be generated without the pointers.

  8. added a commit that references this issue on Jun 8, 2022
    f542a2f
  9. added 4 commits that reference this issue on Jul 15, 2022
    44ac924
    d1c08e4
    c91f581
    34b3268
  10. ghost closed this as completedin #605on Jul 25, 2022
  11. ghost added a commit that references this issue on Jul 25, 2022
    6273e1a
  12. added a commit that references this issue on Jan 16, 2024
    4ed39ee
  13. added a commit that references this issue on May 31, 2024
    3777967
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions