-
Notifications
You must be signed in to change notification settings - Fork 99
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
Comments
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 |
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 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. |
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? |
@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. |
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 {
"parameters": [
{
"name": "limit",
"in": "query",
"description": "maximum number of results to return",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
]
} Setting aside the Maybe using kin to validate just parameters makes more sense and then mapping their errors over to jsonschema proper errors. |
if FYI:
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 I appreciate you taking the time to reply! |
the code you posted works as expected giving the following error: 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 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. |
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 |
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!
The text was updated successfully, but these errors were encountered: