Skip to content

How to catch invalid OpenAPI v3 spec #513

@JeanChristopheMorinPerso

Description

Hello! I'm trying to find how I can make this library to return an error when loading an invalid spec.

Basically, I have an invalid v3 spec that looks like this:

openapi: "3.0.3"
info:
  title: 'My app'
  version: 1.0.0
  description: 'An API'

paths:
  /v1/operation:
    delete:
      summary: Delete something
      responses:
        200:
          description: Success
        default:
          description: '* **400** - Bad Request'
          # Notice here schema is invalid. It should instead be:
          # content:
          #   application/json:
          #     schema:
          #       $ref: '#/components/schemas/Error'
          schema:
            $ref: '#/components/schemas/Error'
components:
  schemas:
    Error:
      type: object
      description: An error response body.
      properties:
        message:
          description: A detailed message describing the error.
          type: string

I load that spec using the openapi3.Loader:

loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true

schema, err := loader.LoadFromFile("/path/to/spec.yaml")
if err != nil {
	fmt.Println("Failed to load swagger_template.yaml:", err.Error())
	os.Exit(1)
}

// Not used in my current example but that's what my code does
schema.InternalizeRefs(context.Background(), nil)

err = schema.Validate(context.Background())
if err != nil {
	fmt.Println("Validation failed:", err.Error())
}

I would expect that either LoadFromFile or Validate return an error saying that it's not a valid spec or something, but it's not.

Is there a way to tell the library to return an error in such cases?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions