-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3627 from swagger-api/ticket-3624
refs #3624 - fix self referenced Optional property
- Loading branch information
Showing
11 changed files
with
326 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
modules/swagger-core/src/main/java/io/swagger/v3/core/util/OptionalUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.swagger.v3.core.util; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import io.swagger.v3.core.converter.AnnotatedType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
|
||
public abstract class OptionalUtils { | ||
|
||
private static Logger LOGGER = LoggerFactory.getLogger(OptionalUtils.class); | ||
|
||
public static boolean _isOptionalType(JavaType jtype) { | ||
|
||
return Arrays.asList("com.google.common.base.Optional", "java.util.Optional") | ||
.contains(jtype.getRawClass().getCanonicalName()); | ||
} | ||
|
||
/** | ||
* check if type is an Optional type, returns the unwrapped type in case, otherwise null | ||
* | ||
* @param type | ||
* | ||
*/ | ||
public static AnnotatedType unwrapOptional(AnnotatedType type) { | ||
|
||
if (type == null) { | ||
return type; | ||
} | ||
try { | ||
final JavaType jtype; | ||
if (type.getType() instanceof JavaType) { | ||
jtype = (JavaType) type.getType(); | ||
} else { | ||
jtype = Json.mapper().constructType(type.getType()); | ||
} | ||
|
||
if (_isOptionalType(jtype)) { | ||
AnnotatedType aType = new AnnotatedType() | ||
.type(jtype.containedType(0)) | ||
.name(type.getName()) | ||
.parent(type.getParent()) | ||
.jsonUnwrappedHandler(type.getJsonUnwrappedHandler()) | ||
.skipOverride(true) | ||
.schemaProperty(type.isSchemaProperty()) | ||
.ctxAnnotations(type.getCtxAnnotations()) | ||
.resolveAsRef(type.isResolveAsRef()) | ||
.jsonViewAnnotation(type.getJsonViewAnnotation()) | ||
.skipSchemaName(type.isSkipSchemaName()) | ||
.skipJsonIdentity(type.isSkipJsonIdentity()) | ||
.propertyName(type.getPropertyName()); | ||
return aType; | ||
} else { | ||
return null; | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Error unwrapping optional", e); | ||
return null; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/Ticket3624Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.swagger.v3.core.resolving; | ||
|
||
import io.swagger.v3.core.converter.AnnotatedType; | ||
import io.swagger.v3.core.converter.ModelConverterContextImpl; | ||
import io.swagger.v3.core.jackson.ModelResolver; | ||
import io.swagger.v3.core.matchers.SerializationMatchers; | ||
import io.swagger.v3.core.util.Yaml; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Optional; | ||
|
||
public class Ticket3624Test extends SwaggerTestBase { | ||
|
||
@Test | ||
public void testSelfReferencingOptional() throws Exception { | ||
|
||
final ModelResolver modelResolver = new ModelResolver(mapper()); | ||
|
||
ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); | ||
|
||
Schema model = context | ||
.resolve(new AnnotatedType(ModelContainer.class)); | ||
|
||
Yaml.prettyPrint(context.getDefinedModels()); | ||
|
||
|
||
SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "Model:\n" + | ||
" type: object\n" + | ||
" properties:\n" + | ||
" model:\n" + | ||
" $ref: '#/components/schemas/Model'\n" + | ||
"ModelContainer:\n" + | ||
" type: object\n" + | ||
" properties:\n" + | ||
" model:\n" + | ||
" $ref: '#/components/schemas/Model'\n"); | ||
|
||
} | ||
|
||
static class ModelContainer { | ||
public Optional<Model> model; | ||
} | ||
|
||
static class Model { | ||
public Optional<Model> model; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/ticket3624/Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.swagger.v3.jaxrs2.resources.ticket3624; | ||
|
||
import io.swagger.v3.jaxrs2.resources.ticket3624.model.ByIdResponse; | ||
import io.swagger.v3.jaxrs2.resources.ticket3624.model.ContainerizedResponse; | ||
import io.swagger.v3.jaxrs2.resources.ticket3624.model.Response; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
@Tag(name = "ExampleService") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Path(Service.ROOT_PATH) | ||
public interface Service { | ||
String ROOT_PATH = "/example"; | ||
|
||
@GET | ||
@Path("/model") | ||
@Operation(summary = " Retrieve models for display to the user") | ||
Response getModels(); | ||
|
||
|
||
@GET | ||
@Path("/model/by/ids") | ||
@Operation(summary = " Retrieve models by their ids") | ||
ByIdResponse getModelsById(); | ||
|
||
@GET | ||
@Path("/containerized/model") | ||
@Operation(summary = " Retrieve review insights for a specific product") | ||
ContainerizedResponse getContainerizedModels(); | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...er-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/ticket3624/model/ByIdResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.swagger.v3.jaxrs2.resources.ticket3624.model; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class ByIdResponse { | ||
public abstract Map<Integer, Model> getModelsById(); | ||
} |
8 changes: 8 additions & 0 deletions
8
.../src/test/java/io/swagger/v3/jaxrs2/resources/ticket3624/model/ContainerizedResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.swagger.v3.jaxrs2.resources.ticket3624.model; | ||
|
||
import java.util.List; | ||
|
||
public abstract class ContainerizedResponse { | ||
public abstract List<ModelContainer> getContainerizedModels(); | ||
public abstract int getTotalCount(); | ||
} |
25 changes: 25 additions & 0 deletions
25
...s/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/ticket3624/model/Model.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.swagger.v3.jaxrs2.resources.ticket3624.model; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
import java.util.Optional; | ||
|
||
|
||
public abstract class Model { | ||
// this is the ID of the review | ||
public abstract int getId(); | ||
|
||
public abstract String getText(); | ||
|
||
public abstract String getTitle(); | ||
|
||
public abstract boolean isActive(); | ||
|
||
public abstract Optional<String> getOptionalString(); | ||
|
||
public abstract Optional<Model> getParent(); | ||
|
||
@Schema | ||
public abstract Optional<Model> getSchemaParent(); | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/ticket3624/model/ModelContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.swagger.v3.jaxrs2.resources.ticket3624.model; | ||
|
||
|
||
import java.util.Optional; | ||
|
||
|
||
public abstract class ModelContainer { | ||
public abstract Optional<Model> getModel(); | ||
public abstract int getId(); | ||
public abstract String getText(); | ||
} |
9 changes: 9 additions & 0 deletions
9
...wagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/ticket3624/model/Response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.swagger.v3.jaxrs2.resources.ticket3624.model; | ||
|
||
import java.util.List; | ||
|
||
public abstract class Response { | ||
public abstract List<Model> getModels(); | ||
|
||
public abstract int getCount(); | ||
} |