Skip to content

Commit

Permalink
fix generating models for inline arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
gcornacchia committed Nov 28, 2024
1 parent 4632508 commit 3c95a31
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
public class BaseJsonSchemaGenerator {

public static final String APPLICATION_JSON = "application/json";
public static final String REQUEST = "request";
public static final String RESPONSE = "response";
@Getter
protected Map<String, JsonNode> generatedObjects = new HashMap<String, JsonNode>();
protected static final String ORIGINAL_REF = "originalRef";
Expand Down Expand Up @@ -77,17 +79,18 @@ private void analyzeOperation(PathItem v) {
} else {
log.warn("code={} response schema is not a referenced definition! type={}", key, r.getContent().get("application/json").getClass());
log.debug("Reference not found, creating it manually");
if (!(sc instanceof ArraySchema)) {
objectsDefinitions.put(op.getOperationId()+"response"+key, sc);
messageObjects.add(op.getOperationId()+"response"+key);
}
String inlineObjectKey = createInlineResponseObjectKey(op,key);
objectsDefinitions.put(inlineObjectKey, sc);
messageObjects.add(inlineObjectKey);
}
}
}
}
}
}



private void findRequestBodySchema(Operation op, Set<String> messageObjects) {
if (op.getRequestBody()!=null) {
if (op.getRequestBody().getContent().get(APPLICATION_JSON)!=null) {
Expand All @@ -99,15 +102,22 @@ private void findRequestBodySchema(Operation op, Set<String> messageObjects) {
} else {
log.warn("Request schema is not a referenced definition!");
log.debug("Ref not found, cresting it manually if object");
if (!(sc instanceof ArraySchema)) {
objectsDefinitions.put(op.getOperationId()+"request", sc);
messageObjects.add(op.getOperationId()+"request");
}
String inlineObjectKey = createInlineRequestObjectKey(op);
objectsDefinitions.put(inlineObjectKey, sc);
messageObjects.add(inlineObjectKey);
}
}
} else {
log.info("No application body of type 'application/json' found for operation {}",op.getOperationId());
}
}
}

private String createInlineRequestObjectKey(Operation op) {
return op.getOperationId()+ REQUEST;
}

private String createInlineResponseObjectKey(Operation op, String responseKey) {
return op.getOperationId()+ RESPONSE +responseKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ private void navigateModel(String originalRef, List<String> usedDefinition, Map<
log.debug("array model={}",res.get(ITEMS));
if (res.get(ITEMS) instanceof Schema) {
Schema s = (Schema) res.get(ITEMS);
navigateModel(s.get$ref(),usedDefinition,res,null);
if (s.get$ref()!=null) {
navigateModel(s.get$ref(), usedDefinition, res, null);
}
}
} else if (ob instanceof ComposedSchema) {
ComposedSchema cm = (ComposedSchema)ob;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ public class TestGenerationFromOAS3 extends AbstractIT {

@Test
public void testOAS3WithAllOf() { testForSwagger("petstoreoas3Allof.json"); }

@Test
public void testOAS3WithInlineArray() { testForSwagger("petstoreoas3ArrayInline.json"); }
}
273 changes: 273 additions & 0 deletions src/test/resources/petstoreoas3ArrayInline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
{
"openapi": "3.0.2",
"info": {
"title": "Swagger Petstore - OpenAPI 3.0",
"description": "This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about\nSwagger at [https://swagger.io](https://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!\nYou can now help us improve the API whether it's by making changes to the definition itself or to the code.\nThat way, with time, we can improve the API in general, and expose some of the new features in OAS3.\n\n_If you're looking for the Swagger 2.0/OAS 2.0 version of Petstore, then click [here](https://editor.swagger.io/?url=https://petstore.swagger.io/v2/swagger.yaml). Alternatively, you can load via the `Edit > Load Petstore OAS 2.0` menu option!_\n\nSome useful links:\n- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)\n- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"email": "[email protected]"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0.11"
},
"externalDocs": {
"description": "Find out more about Swagger",
"url": "http://swagger.io"
},
"servers": [
{
"url": "https://petstore3.swagger.io/api/v3"
}
],
"tags": [
{
"name": "pet",
"description": "Everything about your Pets",
"externalDocs": {
"description": "Find out more",
"url": "http://swagger.io"
}
},
{
"name": "store",
"description": "Access to Petstore orders",
"externalDocs": {
"description": "Find out more about our store",
"url": "http://swagger.io"
}
},
{
"name": "user",
"description": "Operations about user"
}
],
"paths": {
"/pet": {
"put": {
"tags": [
"pet"
],
"summary": "Update an existing pet",
"description": "Update an existing pet by Id",
"operationId": "updatePet",
"requestBody": {
"description": "Update an existent pet in the store",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"photoUrls"
],
"properties": {
"id": {
"type": "integer",
"format": "int64",
"example": 10
},
"name": {
"type": "string",
"example": "doggie"
},
"category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64",
"example": 1
},
"name": {
"type": "string",
"example": "Dogs"
}
},
"xml": {
"name": "category"
}
},
"photoUrls": {
"type": "array",
"xml": {
"wrapped": true
},
"items": {
"type": "string",
"xml": {
"name": "photoUrl"
}
}
},
"tags": {
"type": "array",
"xml": {
"wrapped": true
},
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "tag"
}
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": [
"available",
"pending",
"sold"
]
}
}
}
}
}
}
},
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"photoUrls"
],
"properties": {
"id": {
"type": "integer",
"format": "int64",
"example": 10
},
"name": {
"type": "string",
"example": "doggie"
},
"category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64",
"example": 1
},
"name": {
"type": "string",
"example": "Dogs"
}
},
"xml": {
"name": "category"
}
},
"photoUrls": {
"type": "array",
"xml": {
"wrapped": true
},
"items": {
"type": "string",
"xml": {
"name": "photoUrl"
}
}
},
"tags": {
"type": "array",
"xml": {
"wrapped": true
},
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "tag"
}
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": [
"available",
"pending",
"sold"
]
}
}
}
}
}
}
},
"400": {
"description": "Invalid ID supplied"
},
"404": {
"description": "Pet not found"
},
"422": {
"description": "Validation exception"
}
},
"security": [
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
]
}
}
},
"components": {
"securitySchemes": {
"petstore_auth": {
"type": "oauth2",
"flows": {
"implicit": {
"authorizationUrl": "https://petstore3.swagger.io/oauth/authorize",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
}
},
"api_key": {
"type": "apiKey",
"name": "api_key",
"in": "header"
}
}
}
}

0 comments on commit 3c95a31

Please sign in to comment.