swagger-api / swagger-api/swagger-codegen-generators

Python allOf and readOnly not working

Open
#462 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

When using allOf, the swagger_types and attribute_map is not being generated.

Here is the yaml schema I used:

Click to expand
---
tags:
- name: Model
  description: A model
components:
  schemas:
    Model:
      allOf:
      - type: object
        properties:
          id:
            type: string
            readOnly: true
          name:
            type: string
          description:
            type: string
      - "$ref": "#/components/schemas/Timestamps"
    Timestamps:
      allOf:
      - type: object
        properties:
          created_at:
            readOnly: true
            type: string
            format: date-time
          updated_at:
            readOnly: true
            type: string
            format: date-time
    ErrorResponse:
      type: object
      required:
      - message
      properties:
        message:
          type: string
        errors: {}
paths:
  "/models":
    get:
      tags:
      - model
      summary: Get all Models
      operationId: getModels
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/Model"
        '404':
          description: Model not found
          content: {}
    post:
      tags:
      - model
      summary: Create a new Model
      operationId: createModel
      requestBody:
        description: Model object that will be created.
        content:
          application/json:
            schema:
              type: object
              required:
              - type
              - description
              allOf:
              - "$ref": "#/components/schemas/Model"
        required: true
      responses:
        '201':
          description: Successful operation
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/Model"
        '422':
          description: Invalid input
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponse"
  "/models/{id}":
    parameters:
    - in: path
      name: id
      schema:
        type: string
      required: true
      description: The Model ID.
    get:
      tags:
      - model
      summary: Get Model
      description: Get a Model
      operationId: getModel
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/Model"
        '404':
          description: Model not found
          content: {}
      security:
      - api_key: []
    patch:
      tags:
      - model
      summary: Update a Model
      operationId: updateModel
      requestBody:
        description: Model object with updated values.
        content:
          application/json:
            schema:
              "$ref": "#/components/schemas/Model"
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/Model"
        '404':
          description: Model not found
          content: {}
        '422':
          description: Invalid input
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponse"
    delete:
      tags:
      - model
      summary: Delete a Model
      operationId: deleteModel
      responses:
        '200':
          description: Successful operation
          content: {}
        '404':
          description: Model not found
          content: {}
        '422':
          description: Invalid input
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponse"
openapi: 3.0.2
info:
  title: Test API
  description: Test API docs
  termsOfService: http://swagger.io/terms/
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
externalDocs:
  description: Find out more about Swagger
  url: http://swagger.io
servers:
- url: https://127.0.0.1/api

Without allOf it works, however, properties that specifies readOnly still generates setters:

Click to expand
---
tags:
- name: Model
  description: A model
components:
  schemas:
    Model:
      properties:
        id:
          type: string
          readOnly: true
        name:
          type: string
        description:
          type: string
        created_at:
          readOnly: true
          type: string
          format: date-time
        updated_at:
          readOnly: true
          type: string
          format: date-time
    ErrorResponse:
      type: object
      required:
      - message
      properties:
        message:
          type: string
        errors: {}
paths:
  "/models":
    get:
      tags:
      - model
      summary: Get all Models
      operationId: getModels
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/Model"
        '404':
          description: Model not found
          content: {}
    post:
      tags:
      - model
      summary: Create a new Model
      operationId: createModel
      requestBody:
        description: Model object that will be created.
        content:
          application/json:
            schema:
              type: object
              required:
              - type
              - description
              allOf:
              - "$ref": "#/components/schemas/Model"
        required: true
      responses:
        '201':
          description: Successful operation
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/Model"
        '422':
          description: Invalid input
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponse"
  "/models/{id}":
    parameters:
    - in: path
      name: id
      schema:
        type: string
      required: true
      description: The Model ID.
    get:
      tags:
      - model
      summary: Get Model
      description: Get a Model
      operationId: getModel
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/Model"
        '404':
          description: Model not found
          content: {}
      security:
      - api_key: []
    patch:
      tags:
      - model
      summary: Update a Model
      operationId: updateModel
      requestBody:
        description: Model object with updated values.
        content:
          application/json:
            schema:
              "$ref": "#/components/schemas/Model"
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/Model"
        '404':
          description: Model not found
          content: {}
        '422':
          description: Invalid input
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponse"
    delete:
      tags:
      - model
      summary: Delete a Model
      operationId: deleteModel
      responses:
        '200':
          description: Successful operation
          content: {}
        '404':
          description: Model not found
          content: {}
        '422':
          description: Invalid input
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/ErrorResponse"
openapi: 3.0.2
info:
  title: Test API
  description: Test API docs
  termsOfService: http://swagger.io/terms/
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
externalDocs:
  description: Find out more about Swagger
  url: http://swagger.io
servers:
- url: https://127.0.0.1/api

This is the command I used to generate:

docker run --rm -v `pwd`:/local swaggerapi/swagger-codegen-cli-v3:unstable generate \
    -c /local/codegen/config.json \
    -i /local/swagger.yaml \
    -l python \
    -o /local/codegen/python

Version 3.0.12-SNAPSHOT

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with swagger.yaml and codegen/config.json using the documented Docker command, then inspect the generated files under codegen/python and the Python generator's model templates. Confirm the expected output for allOf properties and readOnly fields, and add or update a regression test if the generator's test suite has coverage for these models.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.