Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map multiple components/schemas to a single openapiv3 document #48

Closed
peteraba opened this issue Nov 15, 2021 · 15 comments
Closed

Map multiple components/schemas to a single openapiv3 document #48

peteraba opened this issue Nov 15, 2021 · 15 comments

Comments

@peteraba
Copy link

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!

@santhosh-tekuri
Copy link
Owner

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
Copy link

lkm commented Feb 10, 2022

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
Copy link

lkm commented Feb 10, 2022

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

@fenollp
Copy link

fenollp commented Feb 11, 2022

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

@lkm
Copy link

lkm commented Feb 11, 2022

@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.

@santhosh-tekuri
Copy link
Owner

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

@santhosh-tekuri
Copy link
Owner

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")
}

@chanced
Copy link

chanced commented Aug 4, 2022

@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.

@santhosh-tekuri
Copy link
Owner

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)
	}

@chanced
Copy link

chanced commented Aug 6, 2022

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!

@santhosh-tekuri
Copy link
Owner

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

@chanced
Copy link

chanced commented Aug 6, 2022

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.

@santhosh-tekuri
Copy link
Owner

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.

@santhosh-tekuri
Copy link
Owner

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

@santhosh-tekuri
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants