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

[bug-fix] Generated OAS contains an invalid property #34

Merged
merged 1 commit into from
Aug 7, 2023

Conversation

skgndi12
Copy link
Contributor

@skgndi12 skgndi12 commented Aug 6, 2023

Describe bug

When specifying content-type as described in the official document, a property not defined in the OpenAPI Specification called mediaType was added as a subproperty of schema. As a result, an invalid OAS was created.

스크린샷 2023-08-06 오후 3 10 40

To reproduce

Condition

  1. Version: 0.1.104
  2. Situation: When specifying content-type with the @mediaType tag above the request body type

Schema and request body type

// schema
export type DevApiSpec = Tspec.DefineApiSpec<{
  basePath: '/api/v1/dev';
  security: 'jwt';
  tags: ['Development'];
  paths: {
    '/greeting': {
      post: {
        summary: 'Greeting',
        body: GreetingV1Request,
        responses: {
          200: GreetingV1Response,
          default: HttpErrorResponse,
        };
      };
    };
  };
}>;

// Request body type
/** @mediaType application/x-www-form-urlencoded */
export interface GreetingV1Request {
  message: string;
}

Generated OAS

{
  "info": {
    "title": "Mr.C API",
    "version": "1.0.0"
  },
  "openapi": "3.0.3",
  "paths": {
    "/api/v1/dev/greeting": {
      "post": {
        "operationId": "DevApiSpec_post_/greeting",
        "tags": [
          "Development"
        ],
        "summary": "Greeting",
        "security": [
          {
            "jwt": []
          }
        ],
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "mediaType": "application/x-www-form-urlencoded",
                "type": "object",
                "properties": {
                  "message": {
                    "type": "string"
                  }
                },
                "additionalProperties": false,
                "required": [
                  "message"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GreetingV1Response"
                }
              }
            }
          },
          "default": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HttpErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/healthz/liveness": {
      "get": {
        "operationId": "HealthApiSpec_get_/liveness",
        "tags": [
          "Health Checks"
        ],
        "summary": "Check for liveness",
        "parameters": [],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LivenessResponse"
                }
              }
            }
          },
          "default": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HttpErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "GreetingV1Response": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          }
        },
        "additionalProperties": false
      },
      "HttpErrorResponse": {
        "type": "object",
        "properties": {
          "type": {
            "$ref": "#/components/schemas/ErrorType"
          },
          "messages": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        },
        "additionalProperties": false,
        "required": [
          "messages",
          "type"
        ]
      },
      "ErrorType": {
        "enum": [
          "BAD_REQUEST",
          "COMMENT_NOT_FOUND",
          "FORBIDDEN",
          "METHOD_NOT_ALLOWED",
          "NOT_ACCEPTABLE",
          "PAYLOAD_TOO_LARGE",
          "REPLY_NOT_FOUND",
          "REVIEW_NOT_FOUND",
          "ROUTE_NOT_FOUND",
          "UNAUTHORIZED",
          "UNEXPECTED",
          "UNSUPPORTED_MEDIA_TYPE",
          "USER_NOT_FOUND"
        ],
        "type": "string"
      },
      "GreetingV1Request": {
        "mediaType": "application/x-www-form-urlencoded",
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          }
        },
        "additionalProperties": false,
        "required": [
          "message"
        ]
      },
      "LivenessResponse": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          }
        },
        "additionalProperties": false,
        "required": [
          "message"
        ]
      }
    },
    "securitySchemes": {
      "jwt": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    }
  }
}

Changes

In the openapiGenerator file, notice that the value of bodyParams is assigned with mediaType included. Modified bodyParams to not include the value by creating and assigning a separate variable called mediaType. I wanted to test this fix, but I couldn't find a test code. Please review if you find any issues.

@hyeonss0417 hyeonss0417 self-requested a review August 7, 2023 03:01
@hyeonss0417 hyeonss0417 added the bug Something isn't working label Aug 7, 2023
Copy link
Contributor

@hyeonss0417 hyeonss0417 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!✅
Thank you for your contribution👍

@hyeonss0417 hyeonss0417 merged commit e78d668 into ts-spec:main Aug 7, 2023
@hyeonss0417
Copy link
Contributor

hyeonss0417 commented Aug 7, 2023

I update tspec version to 0.1.105 including your changes. 🎉

https://github.com/ts-spec/tspec/releases/tag/v0.1.105

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants