fix #18184, check nullable in 3.1 spec (#18189)

This commit is contained in:
William Cheng 2024-03-23 17:15:27 +08:00 committed by GitHub
parent a26c87e7a9
commit 8288b6fb15
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 104 additions and 2 deletions

View File

@ -852,8 +852,14 @@ public class OpenAPINormalizer {
} }
} }
if (!(schema instanceof JsonSchema) && (schema.getType() == null || schema.getType().equals("null")) && schema.get$ref() == null) { if (schema instanceof JsonSchema) { // 3.1 spec
return true; 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` // convert referenced enum of null only to `nullable:true`

View File

@ -513,4 +513,23 @@ public class OpenAPINormalizerTest {
assertEquals(((Schema) schema4.getProperties().get("set_property")).getNullable(), true); assertEquals(((Schema) schema4.getProperties().get("set_property")).getNullable(), true);
assertEquals(((Schema) schema4.getProperties().get("map_property")).getNullable(), null); 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");
}
} }

View File

@ -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