From 14444b4edd4dbb38746acf2258053c10969eddf7 Mon Sep 17 00:00:00 2001 From: frantuma Date: Thu, 28 Sep 2023 10:44:29 +0200 Subject: [PATCH] apply resolve fully to non object properties --- pom.xml | 6 +- .../swagger/inflector/utils/ResolverUtil.java | 2 + .../test/schema/SchemaValidationTest.java | 29 +++++++++ .../swagger/test/utils/ResolverUtilTest.java | 11 ++++ .../swagger/string-property-resolving.yaml | 60 +++++++++++++++++++ 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/test/swagger/string-property-resolving.yaml diff --git a/pom.xml b/pom.xml index b11f610b..feea6850 100644 --- a/pom.xml +++ b/pom.xml @@ -546,9 +546,9 @@ - 1.6.10 - 1.0.65 - 2.14.2 + 1.6.11 + 1.0.67 + 2.15.2 2.12.2 1.9.2 9.4.51.v20230217 diff --git a/src/main/java/io/swagger/inflector/utils/ResolverUtil.java b/src/main/java/io/swagger/inflector/utils/ResolverUtil.java index d9774100..6ea5eded 100644 --- a/src/main/java/io/swagger/inflector/utils/ResolverUtil.java +++ b/src/main/java/io/swagger/inflector/utils/ResolverUtil.java @@ -164,6 +164,8 @@ public Model resolveModel(Model schema) { LOGGER.debug("not adding recursive properties, using generic object"); model.addProperty(key, new ObjectProperty()); } + } else if (System.getenv("resolveFullySchemaObjectOnly") == null && System.getProperty("resolveFullySchemaObjectOnly") == null){ + model.addProperty(key, property); } } return model; diff --git a/src/test/java/io/swagger/test/schema/SchemaValidationTest.java b/src/test/java/io/swagger/test/schema/SchemaValidationTest.java index 40d61e46..1e1f3150 100644 --- a/src/test/java/io/swagger/test/schema/SchemaValidationTest.java +++ b/src/test/java/io/swagger/test/schema/SchemaValidationTest.java @@ -1,10 +1,12 @@ package io.swagger.test.schema; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.TextNode; import com.github.fge.jsonschema.core.report.ProcessingReport; import com.github.fge.jsonschema.main.JsonSchemaFactory; import io.swagger.inflector.schema.SchemaValidator; import io.swagger.util.Json; +import io.swagger.util.Yaml; import org.testng.annotations.Test; import static org.testng.Assert.assertFalse; @@ -96,6 +98,33 @@ public void testValidation() throws Exception { assertTrue(report.isSuccess()); } + @Test + public void testStringPropertyEnumValidation() throws Exception { + String schemaAsString = + "description: Type of the resource assigned\n" + + "example: api\n" + + "type: string\n" + + "enum:\n" + + " - organization\n" + + " - api\n" + + " - domain\n" + + " - template\n" + + " - team\n" + + " - project\n" + + " - portal-product"; + JsonNode schemaObject = Yaml.mapper().readTree(schemaAsString); + JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); + com.github.fge.jsonschema.main.JsonSchema schema = factory.getJsonSchema(schemaObject); + + + JsonNode content = TextNode.valueOf("domain"); + ProcessingReport report = schema.validate(content); + assertTrue(report.isSuccess()); + content = TextNode.valueOf("domainbad"); + report = schema.validate(content); + assertTrue(!report.isSuccess()); + } + static class User { public Long id; public String name; diff --git a/src/test/java/io/swagger/test/utils/ResolverUtilTest.java b/src/test/java/io/swagger/test/utils/ResolverUtilTest.java index 533b6375..8b32f307 100644 --- a/src/test/java/io/swagger/test/utils/ResolverUtilTest.java +++ b/src/test/java/io/swagger/test/utils/ResolverUtilTest.java @@ -10,7 +10,9 @@ import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.ObjectProperty; import io.swagger.models.properties.Property; +import io.swagger.models.properties.StringProperty; import io.swagger.parser.SwaggerParser; +import io.swagger.parser.util.SwaggerDeserializationResult; import io.swagger.sample.models.Dog; import io.swagger.util.Json; import io.swagger.util.Yaml; @@ -302,4 +304,13 @@ public void testResolvingWithoutDefinitions() { Swagger swagger = new SwaggerParser().parse(yaml); new ResolverUtil().resolveFully(swagger); } + + @Test + public void testStringPropertyResolving() throws Exception { + SwaggerDeserializationResult swaggerParseResult = new SwaggerParser().readWithInfo("./src/test/swagger/string-property-resolving.yaml", null, true); + Swagger swagger = swaggerParseResult.getSwagger(); + new ResolverUtil().resolveFully(swagger); + assertTrue(((BodyParameter)swagger.getPath("/orgs/{orgId}/resources").getPost().getParameters().get(1)).getSchema().getProperties().get("type") instanceof StringProperty); + } + } diff --git a/src/test/swagger/string-property-resolving.yaml b/src/test/swagger/string-property-resolving.yaml new file mode 100644 index 00000000..fb7d00d4 --- /dev/null +++ b/src/test/swagger/string-property-resolving.yaml @@ -0,0 +1,60 @@ +swagger: '2.0' +info: + version: 1.0.8 +schemes: + - http +basePath: /access +paths: + '/orgs/{orgId}/resources': + post: + summary: Create a resource + description: >- + Requires system token. Creates a resource for api, domain, organization or team + operationId: addResource + parameters: + - $ref: '#/parameters/orgId' + - in: body + name: resource + required: true + schema: + $ref: '#/definitions/Resource' + responses: + 201: + description: Successfully added resource. +parameters: + orgId: + description: Name of the organization. + name: orgId + in: path + type: string + required: true +definitions: + Resource: + type: object + required: + - organization + - name + - type + properties: + organization: + type: string + description: Organization name. + example: MyOrg + name: + type: string + description: Resource name. + example: Accounts API + type: + $ref: '#/definitions/ResourceType' + ResourceType: + description: Type of the resource assigned + example: api + type: string + enum: + - organization + - api + - domain + - template + - team + - project + - portal-product