instrumenta / openapi2jsonschema

Convert OpenAPI definitions into JSON schemas for all types in the API
Other
226 stars 87 forks source link

Maintain generated JSON schema file name casing from OpenAPI definitions #20

Open heilaaks opened 5 years ago

heilaaks commented 5 years ago

Description

Generated JSON schema files are converted to all lowercase file names. This can break JSON schema references if capital letters are used in OpenAPI reference names.

Expected behavior

Maintain the same casing for generated JSON schema file names as defined in OpenAPI definitions.

Steps to reproduce

# Requires Go version 1.11 to use Go modules.
pip install openapi2jsonschema==0.8.0
openapi2jsonschema schema.yaml -o output
go mod init github.com/random/testing
go build schema.go
./schema
compile error: open /<full-file-path>/output/RequestData.json: no such file or directory
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x6e8558]

goroutine 1 [running]:
github.com/xeipuuv/gojsonschema.(*Schema).validateDocument(0x0, 0x746e40, 0xc0000934a0, 0x0)
        /root/go/pkg/mod/github.com/xeipuuv/gojsonschema@v1.1.0/validation.go:72 +0x78
github.com/xeipuuv/gojsonschema.(*Schema).Validate(0x0, 0x80f880, 0xc00008f880, 0x1, 0x1, 0x63)
        /root/go/pkg/mod/github.com/xeipuuv/gojsonschema@v1.1.0/validation.go:64 +0x6b
main.main()
        /<full-file-path/schema.go:13 +0x1cb
// schema.go
//
// Modify the full file path below to point to generated all.json file.

package main

import (
        "fmt"
        "github.com/xeipuuv/gojsonschema"
)

func main() {
        sl := gojsonschema.NewSchemaLoader()
        schema, err := sl.Compile(
                gojsonschema.NewReferenceLoader("file:///<full-file-path>/output/all.json"))
        fmt.Printf("compile error: %s\n", err)
        document := gojsonschema.NewStringLoader(`{"type": "string"}`)
        result, err := schema.Validate(document)
        if result.Valid() {
                fmt.Printf("document is valid\n")
        } else {
                fmt.Printf("document is not valid\n")
        }
}
# schema.yaml

openapi: 3.0.0
servers:
  - url: /app/v1
info:
  version: "1.0-oas3"
  title: Testing JSON schema generation
paths:
  '/items':
    post:
      summary: post
      requestBody:
        $ref: '#/components/requestBodies/RequestCollection'
      responses:
        '500':
          $ref: '#/components/responses/500'
components:
  schemas:
    RequestCollection:
      type: object
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/RequestData'
      required:
        - data
    RequestData:
      type: object
      properties:
        type:
          type: string
        attributes:
          $ref: '#/components/schemas/Attributes'
    Attributes:
      type: object
      properties:
        name:
          type: string
    Errors:
      type: object
      properties:
        errors:
          type: array
          items:
            $ref: '#/components/schemas/Error'
    Error:
      type: object
      properties:
        status:
          type: string
      required:
        - status
  responses:
    '500':
      description: Internal server error
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Errors'
  requestBodies:
    RequestCollection:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/RequestCollection'
      required: true