santhosh-tekuri/jsonschema

Map multiple components/schemas to a single openapiv3 document

peteraba opened this issue · 15 comments

Hi Santhosh,

Any chance you could help out Pierre regarding this comment?

The problem seems to stem from (*Compiler).AddResource and (*Compiler).Compile() both requiring filenames / URLs though I have to admit I'm really more of a simple messenger here. I'd love to see OpenAPI 3.1 support in kin-openapi. :)

Thanks a bunch!
Cheers!

I am not familiar with openAPI.

it would be helpful, if you can illustrate your issue with sample openAPI document, and what you are trying to accomplish.

i do not understand why AddResource and Compile are not enough for your use-case.

lkm commented

I'm also looking into using this library in kin-openapi. The reason why AddResource isn't sufficient for this use case is that the root openapi document is usually referable by just #, however, the AddResource clearly doesn't allow this behaviour:

func newResource(url string, r io.Reader) (*resource, error) {
	if strings.IndexByte(url, '#') != -1 {
		panic(fmt.Sprintf("BUG: newResource(%q)", url))
	}

An example simplified openapi doc:

components:
  schemas:
    TestObject:
      properties:
        test:
          $ref: "#/components/schemas/my-string"
        name:
          type: string
      type: object
    my-string:
      pattern: "^test-*."
      type: string

If this makes sense to you I wouldn't mind attempting a PR.

lkm commented

Actually I've figured out a way to achieve this with extensions. Very nifty stuff.

@lkm Do you mind sharing your solution or at least the big picture of it?

lkm commented

@fenollp of course I plan to submit a PR to kin-openapi with a proof of concept, just wanted to get it tidied up and pass a few more tests first.

@lkm you do not need to create extension for this. see here how to do this.

sample code for this:

package main

import (
	"fmt"
	"strings"

	"github.com/santhosh-tekuri/jsonschema/v5"
)

var openAPIDocument = `
  {
  "components": {
    "schemas": {
      "TestObject": {
        "properties": {
          "test": {
            "$ref": "#/components/schemas/my-string"
          },
          "name": {
            "type": "string"
          }
        },
        "type": "object"
      },
      "my-string": {
        "pattern": "^test-*.",
        "type": "string"
      }
    }
  }
    }
  `

func main() {
	compiler := jsonschema.NewCompiler()
	if err := compiler.AddResource("openapidocument.json", strings.NewReader(openAPIDocument)); err != nil {
		panic(err)
	}
	schema, err := compiler.Compile("openapidocument.json#/components/schemas/TestObject")
	if err != nil {
		panic(err)
	}
	_ = schema
	fmt.Println("compilation successfull")
}

@santhosh-tekuri That's awesome, thank you!

Do you have any guidance on query parameters which are of type net/url.Values (map[string][]string) but could have a schema such as:

{
  "parameters": [
    {
      "name": "limit",
      "in": "query",
      "description": "maximum number of results to return",
      "required": false,
      "schema": {
        "type": "integer",
        "format": "int32"
      }
    }
  ]
}

Setting aside the required and in, how would you advise handling the schema? Perhaps an extension is needed for this?

Maybe using kin to validate just parameters makes more sense and then mapping their errors over to jsonschema proper errors.

if "schema" is specified for a parameter, then its value must be valid json.

FYI:

  • a simple integer 123 is a valid json
  • a boolean value true and false is a valid json
  • quoted string "hello" is a valid json

so you can use jsonschema to validate. for example the following validates successfully:

	schema := `{"type": "integer"}`
	instance := 123

	sch, err := jsonschema.CompileString("schema.json", schema)
	if err != nil {
		log.Fatalf("%#v", err)
	}

	if err = sch.Validate(instance); err != nil {
		log.Fatalf("%#v", err)
	}

Sorry to hijack this thread with that question.

	schema := `{"type": "integer"}`
	instance := "123"

	sch, err := jsonschema.CompileString("schema.json", schema)
	if err != nil {
		log.Fatalf("%#v", err)
	}

	if err = sch.Validate(instance); err != nil {
		log.Fatalf("%#v", err)
	}

Doesn't fly though. I wasn't expecting your jsonschema package to handle that, I was just curious if you had any ideas on how to handle the potential for url.Values.

I appreciate you taking the time to reply!

the code you posted works as expected giving the following error: expected integer, but got string

check in playground: https://go.dev/play/p/UI0_wpmgEN_x

Right, I know. The challenge is is that query strings always come in as map[string][]string and have a getter method which will retrieve the value as a string if it is a single value.

I wasn't sure if there was a mechanism to potentially treat strings as numbers but I think, ultimately, this is outside the purview of your project.

FWIW, I got partially through implementing a json schema implementation in a different language (rust) that supported up to 2020-12. It is, by no means, an easy feat. You did amazing work and I greatly appreciate your project. Thank you for it.

If my startup is successful, you'll be one of the first I'll help support. Thank you.

convert query value to string, number, boolean and validate each of them against the schema.
if any one of them validates then you can say value is valid against schema.

you can open new discussion from https://github.com/santhosh-tekuri/jsonschema/discussions, to discuss any rather then in issue

@chanced here is trivial implementation https://go.dev/play/p/hErh3XxSECW for validating query value