question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger2; issue making array items nullable

See original GitHub issue

Hi,

I’m evaluating adoption of the Swagger2Module (which seems pretty flexible!) and I noticed that I was unable to apply nullable = true to the items of an array.

Configuration

Dependencies: 4.19.0 Generator:

Swagger2Module swagger2Module = new Swagger2Module();
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09,
    OptionPreset.PLAIN_JSON)
    .with(Option.VALUES_FROM_CONSTANT_FIELDS,
        Option.MAP_VALUES_AS_ADDITIONAL_PROPERTIES,
        Option.FORBIDDEN_ADDITIONAL_PROPERTIES_BY_DEFAULT,
        Option.FLATTENED_OPTIONALS)
    .with(swagger2Module);

POJO:

@ArraySchema(
    arraySchema = @Schema(description = "Description on root field"),
    schema = @Schema(
        nullable = true,
        description = "Description on individual items"))
private Set<String> features = Set.of();

Expectation

"features" : {
    "description" : "Description on root field",
    "type" : "array",
    "items" : {
      "type" : ["string", "null"],
      "description" : "Description on individual items"
    }
}

Generated

"features" : {
    "description" : "Description on root field",
    "type" : "array",
    "items" : {
      "type" : "string",
      "description" : "Description on individual items"
    }
}

Is it clear from my configuration why this is happening? Setting @Schema(nullable = true) seems to work for non-lists/arrays.

Thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
erodewaldcommented, Sep 7, 2021

Fantastic! I go on one little weekend vacation and there it is! 🎉 Thank you @CarstenWickner, I will be sure to reach out if I can identify any issues.

0reactions
CarstenWicknercommented, Sep 4, 2021

The change has just been released in version 4.20.0. The new behavior is only available when explicitly enabling the new Option.NULLABLE_ARRAY_ITEMS_ALLOWED.

Read more comments on GitHub >

github_iconTop Results From Across the Web

x-nullable for array values doesn't have any effect #774 - GitHub
Problem statement If an array/slice value is nil and the aim is to exclude it from the encoded JSON there doesn't seem to...
Read more >
Swagger 2.0 default null for List in models - Stack Overflow
I am trying to build a model object, which contains a "string-list" key-value pair. Is there a way to default the list to...
Read more >
Solved: Array null values - SmartBear Community
Is it possible to define an array that can not contain null? I'm compiling it to Java with Open API 3.0. f.e for...
Read more >
Describing Parameters - Swagger
Path parameters containing arrays and objects can be serialized in different ways: ... OpenAPI 3.0 also supports nullable in schemas, allowing operation ......
Read more >
How to manage nullable properties - Jane - Read the Docs
Most of the time, the schema you get from vendor have issues about nullability of their properties. Here Jane has an option called...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found