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

Schema with @JsonUnwrapped does not include required properties #1060

Closed
twobiers opened this issue Feb 12, 2021 · 2 comments
Closed

Schema with @JsonUnwrapped does not include required properties #1060

twobiers opened this issue Feb 12, 2021 · 2 comments

Comments

@twobiers
Copy link

twobiers commented Feb 12, 2021

*Edit: Tell me If i'm wrong but it seems really to be a bug of swagger-core. I have resolved it locally and will create an PR on their side.

Describe the bug
When a Type is annotated with @JsonUnwrapped and the unwrapped class does have required properties they won't be mapped to the unwrapping Schema.
At first I thought this might be a bug of swagger-core but since Springfox does handle that correctly I think it belongs here?

To Reproduce
As usual I've updated my repository: https://github.com/tobi6112/springdoc-issue-demo

For reference here is it also as code example:

application.properties

springdoc.remove-broken-reference-definitions=false

Bar

public class Bar <T> {
  private T content;

  protected Bar() {
    this.content = null;
  }

  public static <T> Bar<T> of(T content) {
    return of(content);
  }

  @Nullable
  @JsonUnwrapped
  public T getContent() {
    return this.content;
  }
}

Foo

@Data
@AllArgsConstructor
public class Foo {
  @NotNull String foo;
  String secondProperty;
}

FooController

@RestController
public class FooController {

  @GetMapping(value = "foos")
  public ResponseEntity<Bar<Foo>> getFoo() {
    var foo = new Foo("test");
    return ResponseEntity.ok(Bar.of(foo));
  }
}

Expected behavior

"BarFoo": {
  "required": [
    "foo"
  ],
  "type": "object",
  "properties": {
    "foo": {
      "type": "string"
    },
    "secondProperty": {
      "type": "string"
    }
  }
}

Actual behavior

"BarFoo": {
  "type": "object",
  "properties": {
    "foo": {
      "type": "string"
    },
    "secondProperty": {
      "type": "string"
    }
  }
}

Additional Context
I personally have this problem with org.springframework.hateoas.EntityModel since it Unwraps the content.

@ondrejkrpec
Copy link
Contributor

Can confirm this. The generator currently contains a bug that does not copy required properties when using @JsonUnwrapped.

In addition, I would like to ask if it is possible to make the ModelResolver#handleUnwrapped method protected, so it can be overriden in custom model resolver.

Currently there is an issue with using custom JacksonAnnotationIntrospector that alters the properties, because the current implementaion of handleUnwrapped ignores custom introspectors. For instance, if our custom introspector capitalize the property when the @JsonUnwrapped(prefix = "prefix"), the property in the API is not capitalized.

@bnasslahsen
Copy link
Collaborator

@toucheqt and @tobi6112,

Please not this is related to an existing issue with the swagger-core generator, which is already logged in swagger-core project.

You can try to propose a PR on the swagger-core about it.
Once the fix is available, we will ship the last version in springdoc-openapi.

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

3 participants