Skip to content

Commit

Permalink
allow returning 3.1 schema types in 'getType'
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Nov 1, 2022
1 parent 4072cb0 commit cc9be28
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
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"));
if (bindTypes && type == null && types != null && types.size() == 1) {
return types.iterator().next();
}
return type;
}

Expand Down

0 comments on commit cc9be28

Please sign in to comment.