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

Support for oneOf, implementation advice #25

Open
clouetb opened this issue Feb 27, 2022 · 3 comments
Open

Support for oneOf, implementation advice #25

clouetb opened this issue Feb 27, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@clouetb
Copy link

clouetb commented Feb 27, 2022

Hi there,
while trying to generate using your tool (which is great by the way), I'm greeted with the same error depicted in issue #3. It appears that my swagger is containing oneOf (for inheritance implementation purpose).

I looked at the way you're implementing the allOf but it seems the way should be different in this case and I'm a little bit puzzled by the way it should be. Specifically, it seems two approaches may be chosen: either generate one form that would be somewhat polymorphic, or generate one form for each subclass type. Would you mind sharing some implementation advice to let me try to submit a PR?

@martinmcwhorter
Copy link
Contributor

Thanks for this.

I would like to understand this problem better. Can you share your swagger file, or generate a contrived swagger file that reproduces this issue?

@clouetb
Copy link
Author

clouetb commented Mar 6, 2022

Dear Martin,
please find below a trimmed down version of the swagger:

openapi: 3.0.3
info:
  title: Silly walk
  version: 1.0.0
  description: Silly walk app is a monitoring app
paths:
  /sillywalk/checks/:
    get:
      operationId: sillywalk_checks_list
      parameters:
      - name: limit
        required: false
        in: query
        description: Number of results to return per page.
        schema:
          type: integer
      - name: offset
        required: false
        in: query
        description: The initial index from which to return the results.
        schema:
          type: integer
      tags:
      - sillywalk
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PaginatedCheckPolymorphicList'
          description: ''
    post:
      operationId: sillywalk_checks_create
      tags:
      - sillywalk
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CheckPolymorphic'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/CheckPolymorphic'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/CheckPolymorphic'
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CheckPolymorphic'
          description: ''
  /sillywalk/checks/{id}/:
    get:
      operationId: sillywalk_checks_retrieve
      parameters:
      - in: path
        name: id
        schema:
          type: integer
        description: A unique value identifying this check.
        required: true
      tags:
      - sillywalk
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CheckPolymorphic'
          description: ''
    put:
      operationId: sillywalk_checks_update
      parameters:
      - in: path
        name: id
        schema:
          type: integer
        description: A unique value identifying this check.
        required: true
      tags:
      - sillywalk
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CheckPolymorphic'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/CheckPolymorphic'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/CheckPolymorphic'
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CheckPolymorphic'
          description: ''
    patch:
      operationId: sillywalk_checks_partial_update
      parameters:
      - in: path
        name: id
        schema:
          type: integer
        description: A unique value identifying this check.
        required: true
      tags:
      - sillywalk
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PatchedCheckPolymorphic'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/PatchedCheckPolymorphic'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/PatchedCheckPolymorphic'
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CheckPolymorphic'
          description: ''
    delete:
      operationId: sillywalk_checks_destroy
      parameters:
      - in: path
        name: id
        schema:
          type: integer
        description: A unique value identifying this check.
        required: true
      tags:
      - sillywalk
      security:
      - cookieAuth: []
      - basicAuth: []
      - {}
      responses:
        '204':
          description: No response body
components:
  schemas:
    CheckPolymorphic:
      oneOf:
      - $ref: '#/components/schemas/HTTPCheck'
      - $ref: '#/components/schemas/DNSCheck'
      discriminator:
        propertyName: resourcetype
        mapping:
          HTTPCheck: '#/components/schemas/HTTPCheck'
          DNSCheck: '#/components/schemas/DNSCheck'
    DNSCheck:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
          nullable: true
          maxLength: 250
        description:
          type: string
          nullable: true
        frequency:
          type: string
          format: decimal
          pattern: ^\d{0,8}(?:\.\d{0,2})?$
          nullable: true
        polymorphic_ctype:
          type: integer
          readOnly: true
      required:
      - id
      - polymorphic_ctype
    HTTPCheck:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
          nullable: true
          maxLength: 250
        description:
          type: string
          nullable: true
        frequency:
          type: string
          format: decimal
          pattern: ^\d{0,8}(?:\.\d{0,2})?$
          nullable: true
        polymorphic_ctype:
          type: integer
          readOnly: true
      required:
      - id
      - polymorphic_ctype
    PaginatedCheckPolymorphicList:
      type: object
      properties:
        count:
          type: integer
          example: 123
        next:
          type: string
          nullable: true
          format: uri
          example: http://api.example.org/accounts/?offset=400&limit=100
        previous:
          type: string
          nullable: true
          format: uri
          example: http://api.example.org/accounts/?offset=200&limit=100
        results:
          type: array
          items:
            $ref: '#/components/schemas/CheckPolymorphic'
    PatchedCheckPolymorphic:
      oneOf:
      - $ref: '#/components/schemas/PatchedHTTPCheck'
      - $ref: '#/components/schemas/PatchedDNSCheck'
      discriminator:
        propertyName: resourcetype
        mapping:
          HTTPCheck: '#/components/schemas/PatchedHTTPCheck'
          DNSCheck: '#/components/schemas/PatchedDNSCheck'
    PatchedDNSCheck:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
          nullable: true
          maxLength: 250
        description:
          type: string
          nullable: true
        frequency:
          type: string
          format: decimal
          pattern: ^\d{0,8}(?:\.\d{0,2})?$
          nullable: true
        polymorphic_ctype:
          type: integer
          readOnly: true
    PatchedHTTPCheck:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
          nullable: true
          maxLength: 250
        description:
          type: string
          nullable: true
        frequency:
          type: string
          format: decimal
          pattern: ^\d{0,8}(?:\.\d{0,2})?$
          nullable: true
        polymorphic_ctype:
          type: integer
          readOnly: true

I guess I could have trimmed down the swagger a little bit more, but I'm unsure how to do it without harming the consistency of the whole file regarding your generator's need.
The original swagger file is automatically generated by djangorestframework. Polymorphism is the result of the django-rest-polymorphic magic.

@martinmcwhorter
Copy link
Contributor

Thanks for this.

@martinmcwhorter martinmcwhorter added the enhancement New feature or request label Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants