asyncapi / modelina

A library for generating typed models based on inputs such as AsyncAPI, OpenAPI, and JSON Schema documents with high customization
https://modelina.org
Apache License 2.0
300 stars 175 forks source link

Add OpenAPI 3.1 support #1749

Closed jonaslagoni closed 6 months ago

jonaslagoni commented 8 months ago

Reason/Context

Right now we already have support for OpenAPI 3.0 and Swagger 2.0. But we should also support the newest 3.1 version: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md

To support it we need to alter the existing OpenAPI input processor to also support the new 3.1 version: https://github.com/asyncapi/modelina/blob/master/src/processors/OpenAPIInputProcessor.ts

This does

You can use the following OpenAPI 3.1 input document for testing (or come up with your own):

{
  "openapi": "3.1.0",
  "info": {
    "version": "1.0.0",
    "title": "Swagger Petstore",
    "license": {
      "name": "MIT"
    }
  },
  "servers": [
    {
      "url": "http://petstore.swagger.io/v1"
    }
  ],
  "paths": {
    "/pets": {
      "get": {
        "summary": "List all pets",
        "operationId": "listPets",
        "tags": [
          "pets"
        ],
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "description": "How many items to return at one time (max 100)",
            "required": false,
            "schema": {
              "type": "integer",
              "maximum": 100,
              "format": "int32"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "A paged array of pets",
            "headers": {
              "x-next": {
                "description": "A link to the next page of responses",
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pets"
                }
              }
            }
          },
          "default": {
            "description": "unexpected error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create a pet",
        "operationId": "createPets",
        "tags": [
          "pets"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Pet"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Null response"
          },
          "default": {
            "description": "unexpected error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/pets/{petId}": {
      "get": {
        "summary": "Info for a specific pet",
        "operationId": "showPetById",
        "tags": [
          "pets"
        ],
        "parameters": [
          {
            "name": "petId",
            "in": "path",
            "required": true,
            "description": "The id of the pet to retrieve",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Expected response to a valid request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pet"
                }
              }
            }
          },
          "default": {
            "description": "unexpected error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "required": [
          "id",
          "name"
        ],
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
          "name": {
            "type": "string"
          },
          "tag": {
            "type": "string"
          }
        }
      },
      "Pets": {
        "type": "array",
        "maxItems": 100,
        "items": {
          "$ref": "#/components/schemas/Pet"
        }
      },
      "Error": {
        "type": "object",
        "required": [
          "code",
          "message"
        ],
        "properties": {
          "code": {
            "type": "integer",
            "format": "int32"
          },
          "message": {
            "type": "string"
          }
        }
      }
    }
  }
}

Happy to help further if you get stuck on something :v:

Gmin2 commented 8 months ago

Hey @jonaslagoni after making changes i can test this in the playground right?

jonaslagoni commented 8 months ago

Cant remember if playground allows multiple inputs yet.

You can write unit tests for it 😄

Tanay-Verma commented 8 months ago

Hello @jonaslagoni,

I'm eager to contribute to this issue and excited to make my first open-source contribution. As a newcomer, I would greatly appreciate some guidance on the task.

Upon inspecting the OpenAPIInputProcessor.ts file, it appears that the current parser used by the project lacks support for parsing and validating OpenAPI 3.1 specifications. After some research, I came across a repository based on apidevtools/swagger-parser.

readme/openapi-parser

I believe integrating this alternative parser could address the gap in OpenAPI 3.1 support. I'm ready to explore and contribute to implementing this improvement. Any guidance or pointers you can provide would be immensely helpful.

jonaslagoni commented 8 months ago

Sounds like a good idea @Tanay-Verma 👍

Athul0491 commented 8 months ago

We currently aren't using the latest version of the swagger-parser. The latest version of it has support for the OpenAPI 3.1 version as well.

jonaslagoni commented 8 months ago

Sounds even better @Athul0491, @Tanay-Verma 👍

Tanay-Verma commented 8 months ago

We currently aren't using the latest version of the swagger-parser. The latest version of it has support for the OpenAPI 3.1 version as well.

Is this the one you are referring swagger-parser? This is for java 😅.

Athul0491 commented 8 months ago

@Tanay-Verma I was referring to swagger-parser. See this, they have added support for OpenAPI 3.1 in version 10.1.0

Tanay-Verma commented 8 months ago

@Athul0491 @jonaslagoni Do I have to add a class OpenAPIV3_1Schema in OpenAPIV3Schema.ts ? If so would like some pointers on how to go about it.

jonaslagoni commented 8 months ago

Either that or update the existing one for v3 with v3.1 changes as they should be non breaking

asyncapi-bot commented 6 months ago

:tada: This issue has been resolved in version 3.4.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

asyncapi-bot commented 6 months ago

:tada: This issue has been resolved in version 4.0.0-next.5 :tada:

The release is available on:

Your semantic-release bot :package::rocket: