Skip to content

Commit

Permalink
Absent type doesn't imply type object swagger-api#2113
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Salembier committed Jul 30, 2024
1 parent 0ab2e8a commit ccf39a3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ root = true
# Indentation style
# Possible values - tab, space
indent_style = space
end_of_line = lf

# File character encoding
# Possible values - latin1, utf-8, utf-16be, utf-16le
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,6 @@ public Schema resolveSchema(Schema schema) {
Schema property = updated.get(key);

if (property.getProperties() != model.getProperties()) {
if (!hasSchemaType(property)) {
if (SpecVersion.V30.equals(property.getSpecVersion())) {
property.setType("object");
} else {
property.addType("object");
}
}
model.addProperties(key, property);
} else {
LOGGER.debug("not adding recursive properties, using generic object");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,16 @@ public void testIssue1706() {
assertTrue(openAPI.getPaths().get("/resource").getPost().getResponses().get("200").getContent().get("application/json").getSchema() instanceof ObjectSchema);
}

@Test
public void testIssue2113() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);

OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue_2113.yaml", auths, options).getOpenAPI();
ObjectSchema schema = (ObjectSchema) openAPI.getPaths().get("/foo").getPost().getRequestBody().getContent().get("application/json").getSchema();
assertNull(schema.getProperties().get("goo").getType());
}

@Test
public void selfReferenceTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class OpenAPIV31ParserSchemaTest {
protected int serverPort = getDynamicPort();
Expand Down Expand Up @@ -167,54 +165,54 @@ private void tearDownWireMockServer() {
public void test$idUrlExternal() throws Exception {
ParseOptions p = new ParseOptions();
p.setResolve(true);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-external/root.json").getAbsolutePath(), null, p);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-external/root.json").toURI().toASCIIString(), null, p);
compare("$id-uri-external", swaggerParseResult);
}

@Test
public void test$idUrlEnclosing() throws Exception {
ParseOptions p = new ParseOptions();
p.setResolve(true);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-enclosing/root.json").getAbsolutePath(), null, p);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-enclosing/root.json").toURI().toASCIIString(), null, p);
compare("$id-uri-enclosing", swaggerParseResult);
}

@Test
public void test$idUrlDirect() throws Exception {
ParseOptions p = new ParseOptions();
p.setResolve(true);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-direct/root.json").getAbsolutePath(), null, p);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-direct/root.json").toURI().toASCIIString(), null, p);
compare("$id-uri-direct", swaggerParseResult);
}
@Test
public void test$idUrlUnresolvable() throws Exception {
ParseOptions p = new ParseOptions();
p.setResolve(true);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-unresolvable/root.json").getAbsolutePath(), null, p);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-unresolvable/root.json").toURI().toASCIIString(), null, p);
compare("$id-unresolvable", swaggerParseResult);
}

@Test
public void testAnchorExt() throws Exception {
ParseOptions p = new ParseOptions();
p.setResolve(true);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-external/root.json").getAbsolutePath(), null, p);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-external/root.json").toURI().toASCIIString(), null, p);
compare("$anchor-external", swaggerParseResult);
}

@Test
public void testAnchorInt() throws Exception {
ParseOptions p = new ParseOptions();
p.setResolve(true);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-internal/root.json").getAbsolutePath(), null, p);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-internal/root.json").toURI().toASCIIString(), null, p);
compare("$anchor-internal", swaggerParseResult);
}

@Test
public void testAnchorUnresolve() throws Exception {
ParseOptions p = new ParseOptions();
p.setResolve(true);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-not-found/root.json").getAbsolutePath(), null, p);
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-not-found/root.json").toURI().toASCIIString(), null, p);
compare("$anchor-not-found", swaggerParseResult);
}

Expand All @@ -223,7 +221,7 @@ public void compare(String dir, SwaggerParseResult result) throws Exception {
ObjectMapper mapper = Json31.mapper().copy();
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
String actual = mapper.writer(new DefaultPrettyPrinter()).writeValueAsString(result.getOpenAPI());
String actual = mapper.writer(new DefaultPrettyPrinter()).writeValueAsString(result.getOpenAPI()).replace("\r\n", "\n");
org.testng.Assert.assertEquals(actual,
FileUtils.readFileToString(new File("src/test/resources/3.1.0/dereference/schema/" + dir + "/dereferenced.json")));
}
Expand Down
29 changes: 29 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue_2113.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Issue X
servers:
- url: http://petstore.swagger.io/api
paths:
/foo:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Foo'
required: true
responses:
200:
description: ok
components:
schemas:
Foo:
type: object
allOf:
- $ref: "#/components/schemas/Goo"
Goo:
type: object
properties:
goo:
title: "Goo"

0 comments on commit ccf39a3

Please sign in to comment.