Skip to content
Open
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
Expand Up @@ -90,7 +90,7 @@ public static boolean hasSchemaAnnotation(io.swagger.v3.oas.annotations.media.Sc
&& schema.accessMode().equals(io.swagger.v3.oas.annotations.media.Schema.AccessMode.AUTO)
&& !schema.deprecated()
&& schema.allowableValues().length == 0
&& StringUtils.isBlank(schema.defaultValue())
// && StringUtils.isBlank(schema.defaultValue())
&& schema.implementation().equals(Void.class)
&& StringUtils.isBlank(schema.example())
&& StringUtils.isBlank(schema.pattern())
Expand Down Expand Up @@ -597,13 +597,16 @@ public static Optional<Schema> getSchemaFromAnnotation(
Schema existingSchema,
Schema.SchemaResolution schemaResolution,
ModelConverterContext context) {
System.out.println("조건 검사");
System.out.println(schema);
if (schema == null || !hasSchemaAnnotation(schema)) {
if (existingSchema == null || (!openapi31 && Schema.SchemaResolution.DEFAULT.equals(schemaResolution))) {
return Optional.empty();
} else if (existingSchema != null && (openapi31 || Schema.SchemaResolution.INLINE.equals(schemaResolution))) {
return Optional.of(existingSchema);
}
}
System.out.println("조건 통과");
Schema schemaObject = null;
if (!openapi31) {
if (existingSchema != null) {
Expand Down Expand Up @@ -751,8 +754,8 @@ public static Optional<Schema> getSchemaFromAnnotation(
if (schema.examples().length > 0) {
schemaObject.setExamples(Arrays.asList(schema.examples()));
}

if (StringUtils.isNotBlank(schema.defaultValue())) {
if (schema.defaultValue() != null) {
System.out.println(schema.defaultValue());
schemaObject.setDefault(schema.defaultValue());
}
if (StringUtils.isNotBlank(schema.example())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ public void testAllofResolving() {
" - type: object\n" +
" description: First user schema property\n" +
" nullable: true\n" +
" default: \"\"\n" +
" - $ref: \"#/components/schemas/UserProperty\"\n" +
" propertyTwo:\n" +
" allOf:\n" +
" - type: object\n" +
" description: Second user schema property\n" +
" example: example value for propertyTwo\n" +
" default: \"\"\n" +
" - $ref: \"#/components/schemas/UserProperty\"\n" +
" propertyThree:\n" +
" allOf:\n" +
" - type: object\n" +
" description: \"Third user schema property, with example for testing\"\n" +
" example: example value for propertyThree\n" +
" default: \"\"\n"+
" - $ref: \"#/components/schemas/UserProperty\"\n";

SerializationMatchers.assertEqualsToYaml(c.getDefinedModels(), expectedYaml);
Expand All @@ -64,12 +67,14 @@ public void testAllofResolving() {
" - type: object\n" +
" description: First order schema property\n" +
" nullable: true\n" +
" default: \"\"\n" +
" - $ref: \"#/components/schemas/OrderProperty\"\n" +
" userProperty:\n" +
" allOf:\n" +
" - type: object\n" +
" description: \"Order schema property, references UserProperty\"\n" +
" example: example value for userProperty\n" +
" default: \"\"\n" +
" - $ref: \"#/components/schemas/UserProperty\"\n" +
"UserProperty:\n" +
" type: object\n" +
Expand All @@ -83,18 +88,21 @@ public void testAllofResolving() {
" - type: object\n" +
" description: First user schema property\n" +
" nullable: true\n" +
" default: \"\"\n" +
" - $ref: \"#/components/schemas/UserProperty\"\n" +
" propertyTwo:\n" +
" allOf:\n" +
" - type: object\n" +
" description: Second user schema property\n" +
" example: example value for propertyTwo\n" +
" default: \"\"\n" +
" - $ref: \"#/components/schemas/UserProperty\"\n" +
" propertyThree:\n" +
" allOf:\n" +
" - type: object\n" +
" description: \"Third user schema property, with example for testing\"\n" +
" example: example value for propertyThree\n" +
" default: \"\"\n" +
" - $ref: \"#/components/schemas/UserProperty\"\n";
SerializationMatchers.assertEqualsToYaml(c.getDefinedModels(), expectedYaml);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public void testInlineResolving() {
" description: InlineSchemaFirst property 1\n" +
" nullable: true\n" +
" example: example\n" +
" default: \"\"\n" +
" property2:\n" +
" type: object\n" +
" description: ' InlineSchemaFirst property 2'\n" +
" example: example 2\n" +
" default: \"\"\n" +
"InlineSchemaPropertyFirst:\n" +
" type: object\n" +
" description: property\n" +
Expand All @@ -45,10 +47,12 @@ public void testInlineResolving() {
" description: InlineSchemaFirst property 1\n" +
" nullable: true\n" +
" example: example\n" +
" default: \"\"\n" +
" property2:\n" +
" type: object\n" +
" description: ' InlineSchemaFirst property 2'\n" +
" example: example 2\n" +
" default: \"\"\n" +
"InlineSchemaPropertyFirst:\n" +
" type: object\n" +
" description: property\n" +
Expand All @@ -62,10 +66,12 @@ public void testInlineResolving() {
" property1:\n" +
" type: object\n" +
" description: property 1\n" +
" default: \"\"\n" +
" property2:\n" +
" type: object\n" +
" description: property 2\n" +
" example: example\n" +
" default: \"\"\n" +
" description: propertysecond\n" +
" nullable: true\n" +
" example: examplesecond\n" +
Expand All @@ -85,27 +91,33 @@ public void testInlineResolving() {
" property1:\n" +
" type: object\n" +
" description: property 1\n" +
" default: \"\"\n" +
" property2:\n" +
" type: object\n" +
" description: property 2\n" +
" example: example\n" +
" default: \"\"\n" +
" description: InlineSchemaSecond property 1\n" +
" nullable: true\n" +
" example: examplesecond\n" +
" default: \"\"\n" +
" property2:\n" +
" type: object\n" +
" description: InlineSchemaSecond property 2\n" +
" example: InlineSchemaSecond example 2\n" +
" default: \"\"\n" +
"InlineSchemaSimple:\n" +
" type: object\n" +
" properties:\n" +
" property1:\n" +
" type: object\n" +
" description: property 1\n" +
" default: \"\"\n" +
" property2:\n" +
" type: object\n" +
" description: property 2\n" +
" example: example\n";
" example: example\n" +
" default: \"\"\n";
SerializationMatchers.assertEqualsToYaml(c.getDefinedModels(), expectedYaml);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public void testCustomSchemaImplementation() {
" exampleField:\n" +
" type: integer\n" +
" format: int32\n" +
" default: \"\"\n" +
" secondExampleField:\n" +
" type: string\n";
" type: string\n" +
" default: \"\"\n";

Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(ModelWithCustomSchemaImplementationInProperty.class);
SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void testCustomSchemaImplementation() {
" description: Prop description\n" +
" secondExampleFieldWithTypeProp:\n" +
" type: string\n" +
" default: \"\"\n" +
"MyEnum:\n" +
" type: string\n" +
" enum:\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void testOAS31Fields() {
" country:\n" +
" type: string\n" +
" const: United States\n" +
" default: \"\"\n" +
"Client:\n" +
" type: object\n" +
" properties:\n" +
Expand All @@ -89,6 +90,7 @@ public void testOAS31Fields() {
" type: array\n" +
" items:\n" +
" type: string\n" +
" default: \"\"\n" +
" description: accepting country\n" +
" enum:\n" +
" - UNITED_STATES_OF_AMERICA\n" +
Expand All @@ -98,6 +100,7 @@ public void testOAS31Fields() {
" type: array\n" +
" items:\n" +
" $ref: \"#/components/schemas/Currency\"\n" +
" default: \"\"\n" +
" description: available currencies\n" +
" uniqueItems: true\n" +
"Currency:\n" +
Expand All @@ -115,38 +118,46 @@ public void testOAS31Fields() {
" type: array\n" +
" contains:\n" +
" type: string\n" +
" default: \"\"\n" +
" items:\n" +
" type: string\n" +
" maxContains: 10\n" +
" minContains: 1\n" +
" prefixItems:\n" +
" - type: string\n" +
" default: \"\"\n" +
" unevaluatedItems:\n" +
" type: number\n" +
" default: \"\"\n" +
" status:\n" +
" type:\n" +
" - string\n" +
" - number\n" +
" default: \"\"\n" +
" intValue:\n" +
" type: integer\n" +
" format: int32\n" +
" $anchor: intValue\n" +
" $comment: comment at schema property level\n" +
" default: \"\"\n" +
" exclusiveMaximum: 100\n" +
" exclusiveMinimum: 1\n" +
" text:\n" +
" type: string\n" +
" contentEncoding: plan/text\n" +
" contentMediaType: base64\n" +
" default: \"\"\n" +
" encodedString:\n" +
" type: string\n" +
" contentMediaType: application/jwt\n" +
" contentSchema:\n" +
" $ref: \"#/components/schemas/MultipleBaseBean\"\n" +
" default: \"\"\n" +
" address:\n" +
" $ref: \"#/components/schemas/Address\"\n" +
" client:\n" +
" $ref: \"#/components/schemas/Client\"\n" +
" default: \"\"\n" +
" dependentSchemas:\n" +
" creditCard:\n" +
" $ref: \"#/components/schemas/CreditCard\"\n" +
Expand Down Expand Up @@ -189,12 +200,14 @@ public void testOAS31Fields() {
" properties:\n" +
" postalCode:\n" +
" type: string\n" +
" default: \"\"\n" +
" pattern: \"[0-9]{5}(-[0-9]{4})?\"\n" +
"PostalCodePattern:\n" +
" type: object\n" +
" properties:\n" +
" postalCode:\n" +
" type: string\n" +
" default: \"\"\n" +
" pattern: \"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]\"\n" +
"PropertyNamesPattern:\n" +
" pattern: \"^[A-Za-z_][A-Za-z0-9_]*$\"\n");
Expand All @@ -216,6 +229,7 @@ public void testDependentSchemasAnnotation() {
" type: object\n" +
" dependentSchemas:\n" +
" value:\n" +
" default: \"\"\n" +
" properties:\n" +
" enable:\n" +
" $ref: \"#/components/schemas/BooleanFakeClass\"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public void testArraySchemaItemsValidation() {
"properties:\n" +
" startPoint:\n" +
" $ref: '#/components/schemas/GeoPoint'\n" +
" default: \"\"\n" +
" description: Point where the route begins\n" +
" intermediatePoint:\n" +
" $ref: '#/components/schemas/GeoPoint'\n" +
" default: \"\"\n" +
" description: Intermediate point of the route\n" +
" endPoint:\n" +
" $ref: '#/components/schemas/GeoPoint'\n" +
" default: \"\"\n" +
" description: Point where the route ends");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.UUID;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class AnnotationsUtilsTest {
Expand Down Expand Up @@ -65,7 +66,8 @@ private Object[][] expectedSchemaFromTypeAndFormat() {
{"byteType", ImmutableMap.of("type", "string", "format", "byte")},
{"binaryType", ImmutableMap.of("type", "string", "format", "binary")},
{"emailType", ImmutableMap.of("type", "string", "format", "email")},
{"dummyType", ImmutableMap.of("$ref", "#/components/schemas/DummyClass")}
{"dummyType", ImmutableMap.of("$ref", "#/components/schemas/DummyClass")},
{"emptyDefaultValue", ImmutableMap.of("type", "string", "defaultValue", "")}
};
}

Expand All @@ -79,6 +81,11 @@ public void getSchema(String methodName, Map<String, Object> expected) throws No
assertEquals(schema.get().getType(), expected.get("type"));
assertEquals(schema.get().getFormat(), expected.get("format"));
assertEquals(schema.get().get$ref(), expected.get("$ref"));

if (expected.containsKey("defaultValue")) {
assertNotNull(schema.get().getDefault());
assertEquals(schema.get().getDefault(), expected.get("defaultValue"));
}
}

@ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Byte.class)))
Expand All @@ -97,6 +104,9 @@ private void emailType() {
private void dummyType() {
}

class DummyClass implements Serializable {}
@ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(defaultValue = "", type = "string")))
private void emptyDefaultValue() {
}

class DummyClass implements Serializable {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void testSchemaResolutionAllOf() {
" schema:\n" +
" allOf:\n" +
" - description: InlineSchemaSecond API\n" +
" default: \"\"\n" +
" - $ref: \"#/components/schemas/InlineSchemaFirst\"\n" +
" responses:\n" +
" default:\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void testSchemaResolutionAnnotation() {
" nullable: true\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaPropertyFirst\"\n" +
" default: \"\"\n" +
" property2:\n" +
" type: object\n" +
" properties:\n" +
Expand Down Expand Up @@ -110,7 +111,8 @@ public void testSchemaResolutionAnnotation() {
" description: property 2\n" +
" example: example\n" +
" allOf:\n" +
" - $ref: \"#/components/schemas/InlineSchemaPropertySimple\"\n";
" - $ref: \"#/components/schemas/InlineSchemaPropertySimple\"\n" +
" default: \"\"\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
ModelConverters.reset();
}
Expand Down
Loading