Skip to content

Examples not being validated #588

@danicc097

Description

@danicc097

As per #526 I'd expect schema examples to be validated as well, but I can't get these tests to fail validation using either examples or example (this one not implemented afaik). Am I missing something in the code below?

package openapi3

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestExamplesValidation(t *testing.T) {
	spec := []byte(`
openapi: 3.0.0

paths:
  /user:
    post:
      description: User creation.
      operationId: createUser
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/CreateUserRequest"
            examples:
              BadUser:
                $ref: '#/components/examples/BadUser'
        description: Created user object
        required: true
      responses:
        "201":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CreateUserResponse"
          description: successful operation
      # security:
      #   - api_key: []
      summary: creates a new user
      tags:
        - user
components:
  schemas:
    CreateUserRequest:
      description: represents a new User
      example:
        username: "]bad["
        email: bad
        password: short
      required:
        - username
        - email
        - password
      properties:
        username:
          type: string
          pattern: "^[ a-zA-Z0-9_-]+$"
          minLength: 3
        email:
          type: string
          pattern: "^[A-Za-z0-9+_.-]+@(.+)$"
        password:
          type: string
          minLength: 7
      title: a User
      type: object
    CreateUserResponse:
      description: represents the response to a User creation
      properties:
        access_token:
          type: string
        user_id:
          format: int64
          type: integer
      type: object
# implemented in #526
  examples:
    BadUser:
      value:
        username: "]bad["
        email: bad
        password: short
info:
  title: An API
  version: 1.2.3.4
`)

	loader := NewLoader()

	doc, err := loader.LoadFromData(spec)
	require.NoError(t, err)

	err = doc.Validate(loader.Context)
	require.Error(t, err) // FAIL
}

EDIT: well this explains it:

// Validate returns an error if Example does not comply with the OpenAPI spec.
func (example *Example) Validate(ctx context.Context) error {
	return nil // TODO
}

Is this being worked on/planned/would accept a PR for it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions