koxudaxi/fastapi-code-generator

422 Unprocessable Entity with camelCase on path parameter

afonsobspinto opened this issue · 0 comments

When generating code with a template definition that contains operation.snake_case_arguments like:

def {{operation.function_name}}({{operation.snake_case_arguments}}) -> {{operation.response}}:

and using an openapi specification that contains camelCased path parameters like:

GET /subjects/{subjectId}
---
---
openapi: 3.0.2
info:
  title: Natus HFO Tools
  version: 1.0.0
servers:
  - url: /api
    description: ""
paths:
  /subjects/{subjectId}:
    summary: Path used to manage a single Subject.
    description: "The REST endpoint/path used to get, update, and delete single instances\
      \ of an `Subject`.  This path contains `GET`, `PUT`, and `DELETE` operations\
      \ used to perform the get, update, and delete tasks, respectively."
    get:
      tags:
        - crud
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Subject'
          description: Successful response - returns a single `Subject`.
      operationId: getSubject
      summary: Get a Subject
      description: Gets the details of a single instance of a `Subject`.
    parameters:
      - name: subjectId
        description: A unique identifier for a `Subject`.
        schema:
          type: integer
        in: path
        required: true
components:
  schemas:
    Study:
      description: ""
      required:
        - name
      type: object
      properties:
        name:
          description: ""
          type: string
        id:
          description: ""
          type: integer
        status:
          description: Defines the general state of the study
          enum:
            - START
            - ONGOING
            - FINISHED
          type: string
        start:
          format: date-time
          description: start date of the `Study`
          type: string
        mri:
          $ref: '#/components/schemas/ClinicalFile'
          description: Original mri
        leftSurface:
          $ref: '#/components/schemas/ClinicalFile'
          description: ""
        rightSurface:
          $ref: '#/components/schemas/ClinicalFile'
          description: ""
        t1:
          $ref: '#/components/schemas/ClinicalFile'
          description: Preprocess mri at standard resolution
        labels:
          description: ""
          type: arrayand
          items:
            $ref: '#/components/schemas/ClinicalFile'
        jobs:
          description: All relevant jobs related to this study.
          type: array
          items:
            $ref: '#/components/schemas/Job'
    Subject:
      description: ""
      required:
        - name
      type: object
      properties:
        name:
          description: ""
          type: string
        id:
          description: ""
          type: integer
        responsibleId:
          description: Responsible user id
          type: string
        studies:
          description: ""
          type: array
          items:
            $ref: '#/components/schemas/Study'
    ClinicalFile:
      description: ""
      required:
        - type
        - path
      type: object
      properties:
        type:
          description: ""
          enum:
            - NIFTI
            - DICOM
            - MESH
          type: string
        path:
          description: The download path for the file
          type: string
        name:
          description: ""
          type: string
    Job:
      description: Describes a batch/asynchronous process
      required:
        - type
        - id
        - start
      type: object
      properties:
        type:
          description: ""
          enum:
            - MRI_PROCESSING
            - MRI_CT_ALIGNMENT
            - ELECTRODE_EXTRACTION
          type: string
        start:
          format: date-time
          description: ""
          type: string
        status:
          description: ""
          enum:
            - QUEUE
            - RUNNING
            - ERROR
            - COMPLETED
          type: string
        id:
          description: ""
          type: string
tags:
  - name: crud
    description: ""

We end up with a default parameter for subject_id of type Path:

def get_subject(subject_id: int = Path(..., alias='subjectId')) -> Subject:

Due to that, valid requests like:
GET /subjects/1 (made for example from the /docs page of the FastApi application)
are returning the erroneous code 422 Unprocessable Entity.

Replacing subjectId by subject_id in the openapi specification no longer generates default value of type Path and therefore, the requests are successful