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

allow returning 3.1 schema types in 'getType' #4285

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.swagger.v3.core.util;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.callbacks.Callback;
import io.swagger.v3.oas.models.headers.Header;
Expand All @@ -26,5 +31,15 @@ public DeserializationModule31() {
this.addDeserializer(ApiResponses.class, new ApiResponses31Deserializer());
this.addDeserializer(Paths.class, new Paths31Deserializer());
this.addDeserializer(Callback.class, new Callback31Deserializer());

this.setDeserializerModifier(new BeanDeserializerModifier()
{
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
if (beanDesc.getBeanClass() == OpenAPI.class) {
return new OpenAPI31Deserializer(deserializer);
}
return deserializer;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.swagger.v3.core.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ResolvableDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.SpecVersion;

import java.io.IOException;

public class OpenAPI31Deserializer extends StdDeserializer<OpenAPI> implements ResolvableDeserializer {

private final JsonDeserializer<?> defaultDeserializer;

public OpenAPI31Deserializer(JsonDeserializer<?> defaultDeserializer)
{
super(OpenAPI.class);
this.defaultDeserializer = defaultDeserializer;
}

@Override
public OpenAPI deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
OpenAPI openAPI = (OpenAPI) defaultDeserializer.deserialize(jp, ctxt);
openAPI.setSpecVersion(SpecVersion.V31);
return openAPI;
}
@Override public void resolve(DeserializationContext ctxt) throws JsonMappingException {
((ResolvableDeserializer) defaultDeserializer).resolve(ctxt);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.swagger.v3.oas.models.media;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.v3.oas.models.annotations.OpenAPI30;
import io.swagger.v3.oas.models.annotations.OpenAPI31;
import io.swagger.v3.oas.models.Components;
Expand All @@ -26,6 +27,8 @@

public class Schema<T> {

public static final String BIND_TYPE_AND_TYPES = "bind-type";

protected T _default;

private String name;
Expand Down Expand Up @@ -1110,6 +1113,10 @@ public Schema addRequiredItem(String requiredItem) {
**/

public String getType() {
boolean bindTypes = Boolean.valueOf(System.getProperty(BIND_TYPE_AND_TYPES, "false"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseBoolean would have been better than valueOf

if (bindTypes && type == null && types != null && types.size() == 1) {
return types.iterator().next();
}
return type;
}

Expand Down