mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 22:20:56 +00:00
parent
a26c87e7a9
commit
8288b6fb15
@ -852,8 +852,14 @@ public class OpenAPINormalizer {
|
||||
}
|
||||
}
|
||||
|
||||
if (!(schema instanceof JsonSchema) && (schema.getType() == null || schema.getType().equals("null")) && schema.get$ref() == null) {
|
||||
return true;
|
||||
if (schema instanceof JsonSchema) { // 3.1 spec
|
||||
if (Boolean.TRUE.equals(schema.getNullable())) {
|
||||
return true;
|
||||
}
|
||||
} else { // 3.0.x or 2.x spec
|
||||
if ((schema.getType() == null || schema.getType().equals("null")) && schema.get$ref() == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// convert referenced enum of null only to `nullable:true`
|
||||
|
@ -513,4 +513,23 @@ public class OpenAPINormalizerTest {
|
||||
assertEquals(((Schema) schema4.getProperties().get("set_property")).getNullable(), true);
|
||||
assertEquals(((Schema) schema4.getProperties().get("map_property")).getNullable(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() {
|
||||
// to test the rule SIMPLIFY_ONEOF_ANYOF in 3.1 spec
|
||||
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/issue_18184.yaml");
|
||||
// test spec contains anyOf with a ref to enum and another scheme type is null
|
||||
|
||||
Schema schema = openAPI.getComponents().getSchemas().get("Item");
|
||||
assertEquals(((Schema) schema.getProperties().get("my_enum")).getAnyOf().size(), 2);
|
||||
|
||||
Map<String, String> options = new HashMap<>();
|
||||
options.put("SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING", "true");
|
||||
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
|
||||
openAPINormalizer.normalize();
|
||||
|
||||
Schema schema2 = openAPI.getComponents().getSchemas().get("Item");
|
||||
assertEquals(((Schema) schema2.getProperties().get("my_enum")).getAnyOf(), null);
|
||||
assertEquals(((Schema) schema2.getProperties().get("my_enum")).get$ref(), "#/components/schemas/MyEnum");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
openapi: 3.1.0
|
||||
info:
|
||||
title: simplify-oneof-anyof bug repro
|
||||
version: 0.1.0
|
||||
paths:
|
||||
"/items/{item_id}":
|
||||
put:
|
||||
operationId: update_item
|
||||
summary: Update Item
|
||||
parameters:
|
||||
- in: path
|
||||
name: item_id
|
||||
required: true
|
||||
schema:
|
||||
title: Item Id
|
||||
type: integer
|
||||
- in: query
|
||||
name: q
|
||||
required: false
|
||||
schema:
|
||||
title: Q
|
||||
anyOf:
|
||||
- type: string
|
||||
- type: 'null'
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
"$ref": "#/components/schemas/Item"
|
||||
responses:
|
||||
'200':
|
||||
description: Successful Response
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
"$ref": "#/components/schemas/UpdateResponse"
|
||||
components:
|
||||
schemas:
|
||||
Item:
|
||||
title: Item
|
||||
type: object
|
||||
properties:
|
||||
is_offer:
|
||||
title: Is Offer
|
||||
anyOf:
|
||||
- type: boolean
|
||||
- type: 'null'
|
||||
my_enum:
|
||||
anyOf:
|
||||
- "$ref": "#/components/schemas/MyEnum"
|
||||
- type: 'null'
|
||||
name:
|
||||
title: Name
|
||||
type: string
|
||||
required:
|
||||
- name
|
||||
MyEnum:
|
||||
title: MyEnum
|
||||
type: string
|
||||
enum:
|
||||
- value-1
|
||||
- value-2
|
||||
UpdateResponse:
|
||||
properties:
|
||||
id:
|
||||
title: Item Id
|
||||
type: integer
|
||||
name:
|
||||
title: Item Name
|
||||
type: string
|
||||
required:
|
||||
- name
|
||||
- id
|
||||
title: UpdateResponse
|
||||
type: object
|
Loading…
x
Reference in New Issue
Block a user