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

External Spec validation of required properties isn't possible #3257

Closed
Hoovs opened this issue Jul 29, 2019 · 1 comment
Closed

External Spec validation of required properties isn't possible #3257

Hoovs opened this issue Jul 29, 2019 · 1 comment

Comments

@Hoovs
Copy link

Hoovs commented Jul 29, 2019

Hi team,
Not 100% the format so if wrong please let me know.

I am using the 1.5.22 tag of swagger. I am working on building a smaller swagger validator based on this tool chain. I want to produce an error saying you specified a required field but it's not in the definition. I have a swagger spec definition like below:

definitions:
  Thing:
    type: object
    required:
      - id
      - notHere
    properties:
      id:
        type: string
      with_underscore:
        type: string

So I wrote some code like:

Swagger spec = null;
try {
    spec = new SwaggerParser().read(path);
} catch (Exception e) {
// TODO
}

Map<String, Model> definitions = spec.getDefinitions();
Model model = definitions.get("Thing");
if (model instanceof ModelImpl) {
    ModelImpl modelImpl = (ModelImpl) model;
    if (modelImpl.getRequired() != null && modelImpl.getRequired().size() > 0) {
        Map<String, Property> properties = modelImpl.getProperties();
        List<String> requiredProperties = modelImpl.getRequired(); // This returns only id.
        for (String requiredProperty : requiredProperties) {
            if (!properties.containsKey(requiredProperty)) {
                //TODO something here
.....

So I looked at the code and getRequired only returns properties that are on the object that match the required.

Is there a way to make this work?

I added code to ModelImpl like:

@JsonIgnore
public List<String> getSpecSpecifiedRequired() {
    return required;
}

Which does work but is this the best way to do this and what are the odds of something like this making it in?

Thank you again for any help.

frantuma added a commit that referenced this issue Dec 5, 2020
Issue #3257 add json ignored required field
@frantuma
Copy link
Member

frantuma commented Dec 5, 2020

merged #3258

@frantuma frantuma closed this as completed Dec 5, 2020
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

2 participants