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

Add x-enumNames support #1054

Closed

Conversation

RicoSuter
Copy link

First: Thanks for this awesome project, works like a charm...

Reasons for making this change

Add support for the x-enumNames property which has the same semantics as enumNames but uses the x- prefix to declare it as a custom/extension property. I'm maintaining the NJsonSchema C# library which generates these properties from .NET enums. See https://github.com/RSuter/NJsonSchema/wiki/Enums

I think it makes sense to support this scenario because this prefix is required/defined in the JSON Schema spec. It seems that the proposal for the property enumNames has been rejected in favor of const (see below).

Alternatives

There are also const enums: json-schema-org/json-schema-spec#67 (comment)

Spec for const: json-schema-org/json-schema-spec#58

Checklist

  • I'm adding or updating code
    • I've added and/or updated tests
    • I've updated docs if needed
    • I've run npm run cs-format on my branch to conform my code to prettier coding style

@epicfaace
Copy link
Member

@RSuter Thanks for the PR and description! One question: where is x-enumNames defined in the JSON schema spec?

@RicoSuter
Copy link
Author

x-enumNames is not in the spec but so is enumNames which is already supported - enumNames even violates json schema as custom extensions must start with x-. I maintain a json schema library which generate with x-...

@epicfaace
Copy link
Member

That's a good point. It looks like enumNames was added in the past because it was in the v5 draft, and retrospectively it probably shouldn't have been added, given that it would have been better to wait until draft-06 was finalized.

It's worth noting, though, that adding custom attributes technically doesn't violate JSON Schema according to draft-07:

Implementations MAY define additional keywords to JSON Schema. Save for explicit agreement, schema authors SHALL NOT expect these additional keywords to be supported by peer implementations. Implementations SHOULD ignore keywords they do not support.

@RSuter , where do you see in the spec that custom extensions should start with x-? The only place I can find that rule is with Swagger.

@glasserc
Copy link
Contributor

I don't even think enumNames ever even made it into draft 5 -- see #532. The alternative the committee proposed was to use oneOf with const and a title. Maybe the best thing would be to add special support for that? Alternatively, maybe we could add support for finding enumNames in the uiSchema?

@glasserc
Copy link
Contributor

Actually, I guess the support for anyOf and title has existed for a while #581. @RSuter, could you just export it that way instead?

@RicoSuter
Copy link
Author

I really dislike the way json schema handles enums: There is a native/clean way to describe enums but as soon as you need to add more metadata (names) you need to switch to a completely different construct which is less concise and much more verbose... I probably will not switch to oneOf enums as i’m pretty sure that most json schema/swagger/openapi tools (code gens) will not support that...

@RicoSuter RicoSuter changed the title [WIP] Add x-enumNames support Add x-enumNames support Mar 29, 2019
@epicfaace
Copy link
Member

Closing due to inactivity.

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

Successfully merging this pull request may close these issues.

3 participants