Weird YAML tag in section /components/examples of auto-generated OpenAPI spec file
See original GitHub issueThe issue appears when a combination of tools is used:
- spring-boot-starter-web:2.3.1.RELEASE
- springdoc-openapi-ui:1.4.3
- jackson:2.11.0 (comes with SpringBoot)
Example of auto-generated OpenAPI file with unwanted tag
....
componets:
....
examples:
umbrellaExample:
value:
object: !<Type A>
type: TYPE_A
name: x
description: "y"
!<Type A>
doesn’t seem to be a valid YAML tag.
Swagger Online Editor tool also complains about this tag and refuses to render such YAML file.
There’s a working example available at https://github.com/EugeneDrm/open-api-tag-bug
Key notes
- there’s an abstract class (
AbstractObject
) with one or more concrete implementations - the abstract class (
AbstractObject
) has atype
field - annotation
@JsonTypeInfo
is used on the abstract class (AbstractObject
) to properly deserialize concrete implementation(s) into abstract reference - there’s a model class (
Umbrella
) which references the abstract class (AbstractObject
) - an example object for model class (
Umbrella
) is defined in OpenAPIcomponents/examples
section via OpenAPI bean
Umbrella
public class Umbrella {
@Schema(description = "This reference to abstract class causes weird YAML tag to be added", anyOf = ConcreteObjectA.class)
private final AbstractObject object;
...
}
AbstractObject
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "type",
visible = true
)
@JsonSubTypes({
@JsonSubTypes.Type(value = ConcreteObjectA.class, name = "Type A")
})
public abstract class AbstractObject {
private final ConcreteType type;
private final String name;
...
}
Controller
@RestController
public class Controller {
@Operation(summary = "Test Bug", responses = {
@ApiResponse(responseCode = "200", description = "OK",
content = @Content(
schema = @Schema(implementation = Umbrella.class),
examples = @ExampleObject(ref = "#/components/examples/umbrellaExample", name = "Example with weird YAML tag")
)
)
})
@GetMapping(value = "/bug", produces = MediaType.APPLICATION_JSON_VALUE)
public Umbrella bug() {
return new Umbrella(new ConcreteObjectA("a", "b"));
}
}
Application
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public OpenAPI openApi() {
return new OpenAPI()
.components(new Components()
.addExamples("umbrellaExample", new Example().value(new Umbrella(new ConcreteObjectA("x", "y"))))
);
}
}
Expected behavior
- No YAML tag is generated in the examples section
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
OpenAPI Specification - Version 3.0.3 - Swagger
An OpenAPI document that conforms to the OpenAPI Specification is itself a JSON object, which may be represented either in JSON or YAML...
Read more >Keeping OpenAPI DRY and Portable - Stoplight Blog
Bundling, dereferencing, and how to use them in Stoplight Studio and CLI workflows.
Read more >There's No Reason to Write OpenAPI By Hand
Others build the API then generate (or manually produce) API descriptions ... feature called "Annotations", for example Java Annotations.
Read more >OpenAPI Tutorial: How to Automatically Generate Tests for ...
Remember that the input to the *.mustache template files is the OpenAPI json specification of your API. We, therefore, need to add our...
Read more >Parsing an OpenAPI file (Swagger) - YouTube
We go though how to read a OpenAPI specification with the old API reading body content. And we also translate it to the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@EugeneDrm,
Thanks to your clear description, a workaround will be availble on the next release of springdoc-openapi. This fix is already available on the latest snapshot.
@bnasslahsen thanks, this is very helpful. I was able to run the code snippet and see that the issue is coming from swagger library. I’ll submit a new issue in
swagger-core
project. Closing this one.