forked from loafle/openapi-generator-original
[inline model resolver] Add support for inline enum refactoring (#16033)
* add support for inline enum refactoring * minor update * add tests
This commit is contained in:
parent
852eb956db
commit
ac1f5f1e81
@ -455,6 +455,7 @@ Note: Only arrayItemSuffix, mapItemSuffix are supported at the moment.
|
|||||||
There are 2 special values:
|
There are 2 special values:
|
||||||
- `SKIP_SCHEMA_REUSE=true` is a special value to skip reusing inline schemas.
|
- `SKIP_SCHEMA_REUSE=true` is a special value to skip reusing inline schemas.
|
||||||
- `REFACTOR_ALLOF_INLINE_SCHEMAS=true` will restore the 6.x (or below) behaviour to refactor allOf inline schemas into $ref. (v7.0.0 will skip the refactoring of these allOf inline schmeas by default)
|
- `REFACTOR_ALLOF_INLINE_SCHEMAS=true` will restore the 6.x (or below) behaviour to refactor allOf inline schemas into $ref. (v7.0.0 will skip the refactoring of these allOf inline schmeas by default)
|
||||||
|
- `RESOLVE_INLINE_ENUMS=true` will refactor inline enum definitions into $ref
|
||||||
|
|
||||||
## OpenAPI Normalizer
|
## OpenAPI Normalizer
|
||||||
|
|
||||||
|
@ -81,11 +81,13 @@ public class InlineModelResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.inlineSchemaNameDefaults.containsKey("REFACTOR_ALLOF_INLINE_SCHEMAS")) {
|
if (this.inlineSchemaNameDefaults.containsKey("REFACTOR_ALLOF_INLINE_SCHEMAS")) {
|
||||||
if (Boolean.valueOf(this.inlineSchemaNameDefaults.get("REFACTOR_ALLOF_INLINE_SCHEMAS"))) {
|
this.refactorAllOfInlineSchemas = Boolean.valueOf(this.inlineSchemaNameDefaults.get("REFACTOR_ALLOF_INLINE_SCHEMAS"));
|
||||||
this.refactorAllOfInlineSchemas = true;
|
} else {
|
||||||
} else { // set to false
|
// not set so default to null;
|
||||||
this.refactorAllOfInlineSchemas = false;
|
}
|
||||||
}
|
|
||||||
|
if (this.inlineSchemaNameDefaults.containsKey("RESOLVE_INLINE_ENUMS")) {
|
||||||
|
this.resolveInlineEnums = Boolean.valueOf(this.inlineSchemaNameDefaults.get("RESOLVE_INLINE_ENUMS"));
|
||||||
} else {
|
} else {
|
||||||
// not set so default to null;
|
// not set so default to null;
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import io.swagger.v3.oas.models.OpenAPI;
|
|||||||
import io.swagger.v3.oas.models.Operation;
|
import io.swagger.v3.oas.models.Operation;
|
||||||
import io.swagger.v3.oas.models.PathItem;
|
import io.swagger.v3.oas.models.PathItem;
|
||||||
import io.swagger.v3.oas.models.media.*;
|
import io.swagger.v3.oas.models.media.*;
|
||||||
|
import io.swagger.v3.oas.models.parameters.Parameter;
|
||||||
import io.swagger.v3.oas.models.parameters.RequestBody;
|
import io.swagger.v3.oas.models.parameters.RequestBody;
|
||||||
import io.swagger.v3.oas.models.responses.ApiResponse;
|
import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||||
import io.swagger.v3.oas.models.responses.ApiResponses;
|
import io.swagger.v3.oas.models.responses.ApiResponses;
|
||||||
@ -1129,4 +1130,45 @@ public class InlineModelResolverTest {
|
|||||||
assertTrue((Schema) schema.getAnyOf().get(0) instanceof StringSchema);
|
assertTrue((Schema) schema.getAnyOf().get(0) instanceof StringSchema);
|
||||||
assertTrue((Schema) schema.getAnyOf().get(1) instanceof IntegerSchema);
|
assertTrue((Schema) schema.getAnyOf().get(1) instanceof IntegerSchema);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void resolveOperationInlineEnum() {
|
||||||
|
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/inline_model_resolver.yaml");
|
||||||
|
Parameter parameter = openAPI.getPaths().get("/resolve_parameter_inline_enum").getGet().getParameters().get(0);
|
||||||
|
assertNull(((ArraySchema) parameter.getSchema()).getItems().get$ref() );
|
||||||
|
|
||||||
|
InlineModelResolver resolver = new InlineModelResolver();
|
||||||
|
Map<String, String> inlineSchemaNameDefaults = new HashMap<>();
|
||||||
|
inlineSchemaNameDefaults.put("RESOLVE_INLINE_ENUMS", "true");
|
||||||
|
resolver.setInlineSchemaNameDefaults(inlineSchemaNameDefaults);
|
||||||
|
resolver.flatten(openAPI);
|
||||||
|
|
||||||
|
Parameter parameter2 = openAPI.getPaths().get("/resolve_parameter_inline_enum").getGet().getParameters().get(0);
|
||||||
|
assertEquals("#/components/schemas/resolveParameterInlineEnum_status_inline_enum_parameter_inner",
|
||||||
|
((ArraySchema) parameter2.getSchema()).getItems().get$ref() );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void resolveOperationInlineEnumFormParameters() {
|
||||||
|
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/inline_model_resolver.yaml");
|
||||||
|
Schema requestBody = openAPI.getPaths().get("/resolve_parameter_inline_enum_form_parameters").getPost().getRequestBody().getContent().get("application/x-www-form-urlencoded").getSchema();
|
||||||
|
assertNull(requestBody.get$ref());
|
||||||
|
|
||||||
|
InlineModelResolver resolver = new InlineModelResolver();
|
||||||
|
Map<String, String> inlineSchemaNameDefaults = new HashMap<>();
|
||||||
|
inlineSchemaNameDefaults.put("RESOLVE_INLINE_ENUMS", "true");
|
||||||
|
resolver.setInlineSchemaNameDefaults(inlineSchemaNameDefaults);
|
||||||
|
resolver.flatten(openAPI);
|
||||||
|
|
||||||
|
Schema requestBody2 = openAPI.getPaths().get("/resolve_parameter_inline_enum_form_parameters").getPost().getRequestBody().getContent().get("application/x-www-form-urlencoded").getSchema();
|
||||||
|
assertEquals("#/components/schemas/resolve_parameter_inline_enum_form_parameters_request", requestBody2.get$ref());
|
||||||
|
|
||||||
|
Schema inlineFormParaemter = (Schema) openAPI.getComponents().getSchemas().get("resolve_parameter_inline_enum_form_parameters_request");
|
||||||
|
assertNotNull(inlineFormParaemter);
|
||||||
|
assertEquals(2, inlineFormParaemter.getProperties().size());
|
||||||
|
assertEquals("#/components/schemas/resolve_parameter_inline_enum_form_parameters_request_enum_form_string",
|
||||||
|
((Schema) inlineFormParaemter.getProperties().get("enum_form_string")).get$ref());
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,58 @@ info:
|
|||||||
servers:
|
servers:
|
||||||
- url: http://api.example.xyz/v1
|
- url: http://api.example.xyz/v1
|
||||||
paths:
|
paths:
|
||||||
|
/resolve_parameter_inline_enum:
|
||||||
|
get:
|
||||||
|
parameters:
|
||||||
|
- name: status_inline_enum
|
||||||
|
in: query
|
||||||
|
description: Status values that need to be considered for filter
|
||||||
|
required: true
|
||||||
|
style: form
|
||||||
|
explode: false
|
||||||
|
deprecated: true
|
||||||
|
schema:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
enum:
|
||||||
|
- available
|
||||||
|
- pending
|
||||||
|
- sold
|
||||||
|
default: available
|
||||||
|
operationId: resolveParameterInlineEnum
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
/resolve_parameter_inline_enum_form_parameters:
|
||||||
|
post:
|
||||||
|
requestBody:
|
||||||
|
content:
|
||||||
|
application/x-www-form-urlencoded:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
enum_form_string_array:
|
||||||
|
description: Form parameter enum test (string array)
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
default: $
|
||||||
|
enum:
|
||||||
|
- '>'
|
||||||
|
- $
|
||||||
|
enum_form_string:
|
||||||
|
description: Form parameter enum test (string)
|
||||||
|
type: string
|
||||||
|
enum:
|
||||||
|
- _abc
|
||||||
|
- '-efg'
|
||||||
|
- (xyz)
|
||||||
|
default: '-efg'
|
||||||
|
operationId: resolve_parameter_inline_enum_form_parameters
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
/resolve_request_body_invalid_ref:
|
/resolve_request_body_invalid_ref:
|
||||||
post:
|
post:
|
||||||
requestBody:
|
requestBody:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user