Skip to content

encoding/jsonschema: OpenAPI schema versions #3375

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

Closed
rogpeppe opened this issue Aug 16, 2024 · 2 comments
Closed

encoding/jsonschema: OpenAPI schema versions #3375

rogpeppe opened this issue Aug 16, 2024 · 2 comments

Comments

@rogpeppe
Copy link
Member

What version of CUE are you using (cue version)?

$ cue version
v0.10.0

Does this issue reproduce with the latest stable release?

Yes

What did you do?

! exec cue def cue+openapi: openapi.cue
stderr 'constraint "oneOf" is not supported'

! exec cue def cue+jsonschema: jsonschema.cue
stderr 'constraint "nullable" is not supported'

-- openapi.cue --
openapi: "3.0.0"
info: {
	version:     "1.0.0"
	title:       "Sample API"
	description: "Some description"
}
components: schemas: Example: {
	type: "object"
	properties: {
		foo: {
			type:        "string"
			nullable:    true
		}
		bar: {
			oneOf: [{
				type: "string"
			}, {
				const: null
			}]
		}
	}
}

-- jsonschema.cue --
$schema: "http://json-schema.org/draft-07/schema#"
type: "object"
properties: {
	foo: {
		type:        "string"
		nullable:    true
	}
	bar: {
		oneOf: [{
			type: "string"
		}, {
			const: null
		}]
	}
}

What did you expect to see?

A passing test.

What did you see instead?

> ! exec cue def cue+openapi: openapi.cue
[stdout]
info: {
	version:     *"1.0.0" | string
	title:       *"Sample API" | string
	description: "Some description"
}
#Example: {
	foo?: null | string
	bar?: string | null
	...
}
FAIL: /tmp/testscript662380999/x.txtar/script.txtar:1: unexpected command success

This schema should not be valid as OpenAPI 3.0 (oneOf is not supported) or JSON Schema (nullable is not supported).

We should have version-specific behaviour for OpenAPI.

@myitcv
Copy link
Member

myitcv commented Aug 19, 2024

We should have version-specific behaviour for OpenAPI.

And accordingly a means of choosing that version via cmd/cue or the Go API.

cueckoo pushed a commit that referenced this issue Sep 4, 2024
Although OpenAPI 3.0 is its own fork of JSON Schema, with distinct
semantics (new and removed keywords, different semantics for other
keywords), `encoding/jsonschema` does not currently have any way of
choosing OpenAPI-specific behaviour.

Fix that by adding an OpenAPI version. As it's not in the linear
progression of other JSON Schema versions (OpenAPI moved to using
exactly JSON Schema 2020-12 in 3.1), we treat it distinctly, requiring
all keywords to opt into it explicitly. This in turn means that almost
all keywords require their version set to be specified explicitly, so it
seems like there's no longer much benefit to having the vanilla `p0`,
`p1` etc constraint functions, so we change to passing the version set
for all constraints. While we're about it, remove `todo` and use the
regular `p1` function so that all the constraint names line up nicely.

Finally we change `encoding/openapi` to choose the correct version based
on the value of the `openapi` field.

For #3375

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I0070f8c02a9b403e2018b84919b886b0bc5f29d8
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200578
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
@rogpeppe
Copy link
Member Author

I think we can treat this as closed by https://cuelang.org/cl/1200578. Further work will be required to implement the OpenAPI variations correctly, but that can be tracked in individual issues.

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

No branches or pull requests

2 participants