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

Open access to schema transformations #18

Closed
nebehr opened this issue Jan 25, 2021 · 4 comments
Closed

Open access to schema transformations #18

nebehr opened this issue Jan 25, 2021 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@nebehr
Copy link

nebehr commented Jan 25, 2021

In some cases it may be useful to be able to register custom schema transformations or otherwise transform schema object prior to validation. Possible use cases:

  • correct spelling of some properties in Swagger schema (e.g. "x-oneof" to "x-oneOf")
  • add "type" property to schema definitions that don't have it but otherwise do describe proper objects ("type" is not a required property according to JSON Schema specification but without it most glaring discrepancies, such as giving string value to an object property, go undetected by validator)

Granted, these use cases would be most properly addressed by correcting the Swagger schema itself but in some cases it is beyond our control.

One option to achieve this would be by opening public access to SwaggerValidator(JsonNode) constructor or otherwise allow to pass already processed JsonNode to the builder.

@bjansen
Copy link
Owner

bjansen commented Jan 25, 2021

Would a new builder method like someone suggested in #15 be enough?

try (InputStream in = getClass().getResourceAsStream("/htdoc/annotations.yaml")) {
    JsonNode swaggerSpec = Yaml.mapper().readTree(in);
    validator = SwaggerValidator.forJsonSchema(swaggerSpec);
}

@bjansen bjansen added the enhancement New feature or request label Jan 25, 2021
@nebehr
Copy link
Author

nebehr commented Jan 25, 2021

Yes, this would allow to cover the use cases I mentioned although it would be nice not to have to re-implement transformations logic since it is already there (use case # 1 above).

Wouldn't it also make sense to expose schema factory to be able to register custom keywords if necessary?

@bjansen bjansen added this to the v1.0.0 milestone Jan 25, 2021
@bjansen
Copy link
Owner

bjansen commented Jan 25, 2021

Sure, I can do that too.

@bjansen
Copy link
Owner

bjansen commented Jan 25, 2021

Here's how it will look:

JsonNode schema = Json.mapper().readTree(getClass().getResourceAsStream("schema.json"));
Map<String, String> transformations = Map.of("x-oneof", "x-oneOf");

SwaggerValidator validator = SwaggerValidator.forJsonNode(schema, transformations);

Custom transformations will be applied before built-in ones, so in this case it will go x-oneof -> x-oneOf -> oneOf.

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

No branches or pull requests

2 participants