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.

Ability to define different schemas for the same class

See original GitHub issue

I’ve run into a problem trying to reuse the same DTO under different OpenAPI schemas. When I have this hierarchy:

@Data
public class Company {
    @Schema(description = "name of the company")
    private String name;
}

@Data
public class Contract {
    @Schema(description = "unique id")
    private String id;

    @Schema(description = "client company")
    private Company client;

    @Schema(description = "partner company")
    private Company partner;
}}

then Company schema will only be exported once under the name “partner company” in the yml file. This means that the field “client” will have the description “partner company”, which makes no sense in this case. I tried giving each field a different name in the Schema annotation, but it seems to be on purpose that you can rewrite a class’ schema this way. Why is that?

I’d really like a solution to this problem, because I can’t give the client a document with errors like this.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
bnasslahsencommented, May 29, 2020

@tobre6,

Because we rely on swagger-core project, currently complex (object) payloads are always resolved as a reference to a schema defined in components.

For your problem, you can set up 2 different schemas as follow:

@Bean
public OpenAPI customOpenAPI() {
	return new OpenAPI().components(new Components()
					.addSchemas("WorkAddressSchema", getSchemaWithDifferentDescription(Address.class, "work Address" ))
					.addSchemas("HomeAddressSchema", getSchemaWithDifferentDescription(Address.class, "home Address" )));
}

private Schema getSchemaWithDifferentDescription(Class className, String description){
	ResolvedSchema resolvedSchema = ModelConverters.getInstance()
			.resolveAsResolvedSchema(
					new AnnotatedType(className).resolveAsRef(false));
	return resolvedSchema.schema.description(description);
}

public class PersonDTO {

	@JsonProperty
	private String email;

	@JsonProperty
	private String firstName;

	@JsonProperty
	private String lastName;

	@Schema(ref = "WorkAddressSchema")
	@JsonProperty
	private Address workAddress;

	@Schema(ref = "HomeAddressSchema")
	@JsonProperty
	private Address homeAddress;
	
}

public class Address {

	@JsonProperty
	private String addressName;

}

This produces, the result you expect: image

2reactions
tobre6commented, May 29, 2020

I vote this as well. Type of object doesn’t define exact use of it:

@Valid @Schema(description = “Net weight of the shipment”) private WeightModel netWeight; @Valid @Schema(description = “The gross weight of the shipment, in KG-s”) private WeightModel grossWeight;

image
Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple Types - JSON Schema
The basic declaration of multiple types is through the "type" keyword, ... In general, when multiple (or no) types are defined in the...
Read more >
OpenAPI - different schema per operation on same resource
I'm using OpenAPI 3.0 to describe an API where each resource has a subtly different schema based on its operation ...
Read more >
Multiple Schema - an overview | ScienceDirect Topics
Multiple schemas are grouped into catalogs, which can then be grouped into ... Interoperability refers to the ability of multiple systems with different ......
Read more >
When Do Schemas And Classes Deviate - Wiki
Database and schemas differ from each other by their nature. ... 1) We shall depart from the simplest case: a schema with one...
Read more >
Schema in Psychology: Definition, Types, Examples
A schema is a cognitive framework or concept that helps organize and interpret information. We use schemas because they allow us to take ......
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