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

Duplicate entries in required #499

Closed
jochenberger opened this issue Dec 6, 2024 · 3 comments · Fixed by #504
Closed

Duplicate entries in required #499

jochenberger opened this issue Dec 6, 2024 · 3 comments · Fixed by #504

Comments

@jochenberger
Copy link
Contributor

I'm creating a schema for my entity with the JakartaValidationModule and Swagger2Module. I have an interface that has a method annotated with @Schema(requiredMode = REQUIRED). The interface has a sub-interface and there's a class implementing the sub-interface. Neither have additional validation or schema annotations.
In the schema, the property is contained in the required array twice.
I'm still trying to find out why, please let me know if you need additional information.

@jochenberger
Copy link
Contributor Author

Alright, it's not about the interfaces. Here's a repro:

Here's a repro:

package org.example;

import java.io.IOException;
import java.util.Set;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.victools.jsonschema.generator.Module;
import com.github.victools.jsonschema.generator.Option;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.github.victools.jsonschema.module.swagger2.Swagger2Module;

import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;

public class SchemaTest {

    public static void main(String[] args) throws IOException {
        SchemaGenerator schemaGenerator = createSchemaGenerator();
        ObjectNode schema = schemaGenerator.generateSchema(Bar.class);
        Json.pretty().writeValue(System.out, schema);
    }

    private static SchemaGenerator createSchemaGenerator() {
        Module swagger2Module = new Swagger2Module();
        SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(
                SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON).with(swagger2Module).with(
                        Option.INLINE_ALL_SCHEMAS, Option.NONSTATIC_NONVOID_NONGETTER_METHODS,
                        Option.FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS);

        SchemaGeneratorConfig config = configBuilder.build();
        return new SchemaGenerator(config);
    }

    interface Foo {
        @Schema(requiredMode = RequiredMode.REQUIRED)
        String getFoo();
    }

    public class Bar {
        @ArraySchema(schema = @Schema(allOf = Foo.class))
        public Set<Foo> getFoos() {
            return null;
        }
    }
}

The output is:

{
  "$schema" : "https://json-schema.org/draft/2019-09/schema",
  "type" : "object",
  "properties" : {
    "foos" : {
      "type" : "array",
      "items" : {
        "type" : "object",
        "properties" : {
          "foo" : {
            "type" : "string"
          }
        },
        "required" : [ "foo", "foo" ]
      }
    }
  }
}

I'm using the ArraySchema/allOf construct to try and work around swagger-api/swagger-core#3900.

@CarstenWickner
Copy link
Member

Hi @jochenberger,

Yeah, the @Schema(allOf) seems to add the second foo in the required array as a result of the "all-of-cleanup" step at the end of the schema generation. I didn't properly consider duplicates there it seems.

Maybe something like this would fix it: main...avoid-duplicate-array-items
I'm a bit short on time these days to properly test it.

jochenberger added a commit to jochenberger/jsonschema-generator that referenced this issue Dec 10, 2024
@jochenberger
Copy link
Contributor Author

jochenberger commented Dec 10, 2024

lgtm.
I added a test in #503.

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

Successfully merging a pull request may close this issue.

2 participants