Update inline model resolver to flatten responses (#19992)

* update inline model resolver flatten responses

* minor update

* minor update

* minor update

* minor update
This commit is contained in:
William Cheng 2024-10-30 11:50:29 +08:00 committed by GitHub
parent 25b6fd3937
commit cbc64e86d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 109 additions and 20 deletions

View File

@ -107,6 +107,7 @@ public class InlineModelResolver {
flattenPaths();
flattenComponents();
flattenComponentResponses();
}
/**
@ -568,6 +569,20 @@ public class InlineModelResolver {
}
}
/**
* Flatten inline models in the responses section in the components.
*/
private void flattenComponentResponses() {
Map<String, ApiResponse> apiResponses = openAPI.getComponents().getResponses();
if (apiResponses == null) {
return;
}
for (Map.Entry<String, ApiResponse> entry : apiResponses.entrySet()) {
flattenContent(entry.getValue().getContent(), null);
}
}
/**
* Flattens properties of inline object schemas that belong to a composed schema into a
* single flat list of properties. This is useful to generate a single or multiple

View File

@ -287,6 +287,7 @@ public class OpenAPINormalizer {
normalizeInfo();
normalizePaths();
normalizeComponentsSchemas();
normalizeComponentsResponses();
}
/**
@ -445,13 +446,20 @@ public class OpenAPINormalizer {
}
for (Map.Entry<String, ApiResponse> responsesEntry : responses.entrySet()) {
if (responsesEntry.getValue() == null) {
continue;
} else {
normalizeContent(ModelUtils.getReferencedApiResponse(openAPI, responsesEntry.getValue()).getContent());
normalizeHeaders(ModelUtils.getReferencedApiResponse(openAPI, responsesEntry.getValue()).getHeaders());
normalizeResponse(responsesEntry.getValue());
}
}
/**
* Normalizes schemas in ApiResponse
*
* @param apiResponse API response
*/
private void normalizeResponse(ApiResponse apiResponse) {
if (apiResponse != null) {
normalizeContent(ModelUtils.getReferencedApiResponse(openAPI, apiResponse).getContent());
normalizeHeaders(ModelUtils.getReferencedApiResponse(openAPI, apiResponse).getHeaders());
}
}
/**
@ -502,6 +510,20 @@ public class OpenAPINormalizer {
}
}
/**
* Normalizes schemas in component's responses.
*/
private void normalizeComponentsResponses() {
Map<String, ApiResponse> apiResponses = openAPI.getComponents().getResponses();
if (apiResponses == null) {
return;
}
for (Map.Entry<String, ApiResponse> entry : apiResponses.entrySet()) {
normalizeResponse(entry.getValue());
}
}
/**
* Auto fix a self referencing schema using any type to replace the self-referencing sub-item.
*
@ -993,7 +1015,6 @@ public class OpenAPINormalizer {
}
/**
* If the schema is oneOf and the sub-schemas is null, set `nullable: true`
* instead.
@ -1113,6 +1134,7 @@ public class OpenAPINormalizer {
schema.setNullable(true);
return schema;
}
/**
* Set nullable to true in map if needed.
*

View File

@ -352,6 +352,14 @@ public class InlineModelResolverTest {
assertTrue(user.getProperties().get("city") instanceof StringSchema);
}
@Test
public void resolveComponentsResponses() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/inline_model_resolver.yaml");
new InlineModelResolver().flatten(openAPI);
ApiResponse apiResponse = openAPI.getComponents().getResponses().get("JustAnotherResponse");
assertEquals(apiResponse.getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/inline_object");
}
@Test
public void resolveRequestBodyInvalidRef() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/invalid_ref_request_body.yaml");

View File

@ -16,10 +16,12 @@
package org.openapitools.codegen;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.responses.ApiResponse;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.annotations.Test;
@ -794,4 +796,22 @@ public class OpenAPINormalizerTest {
assertEquals(((Schema) schema2.getProperties().get("property2")).getAllOf(), null);
assertEquals(((Schema) schema2.getProperties().get("property2")).getAllOf(), null);
}
@Test
public void testOpenAPINormalizerComponentsResponses31Spec() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/common-parameters.yaml");
ApiResponse apiResponse = openAPI.getComponents().getResponses().get("JustAnotherResponse");
assertEquals(((Schema) apiResponse.getContent().get("application/json").getSchema().getProperties().get("uuid")).getType(), null);
assertEquals(((Schema) apiResponse.getContent().get("application/json").getSchema().getProperties().get("label")).getType(), null);
Map<String, String> inputRules = Map.of(
"NORMALIZE_31SPEC", "true"
);
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules);
openAPINormalizer.normalize();
ApiResponse apiResponse2 = openAPI.getComponents().getResponses().get("JustAnotherResponse");
assertEquals(((Schema) apiResponse2.getContent().get("application/json").getSchema().getProperties().get("uuid")).getType(), "integer");
assertEquals(((Schema) apiResponse2.getContent().get("application/json").getSchema().getProperties().get("label")).getType(), "string");
}
}

View File

@ -455,6 +455,18 @@ paths:
type: string
components:
requestBodies: {}
responses:
JustAnotherResponse:
description: just another response
content:
application/json:
schema:
type: object
properties:
uuid:
type: integer
label:
type: string
schemas:
Users:
type: array

View File

@ -76,6 +76,18 @@ paths:
security:
- api_key: []
components:
responses:
JustAnotherResponse:
description: JustAnotherResponse
content:
application/json:
schema:
type: object
properties:
uuid:
type: integer
label:
type: string
requestBodies:
Pet:
content: