Fix inline allOf in request body (#12348)

* fix inline allof in request body

* update samples
This commit is contained in:
William Cheng
2022-05-12 12:51:36 +08:00
committed by GitHub
parent 5867845419
commit d135f1992e
12 changed files with 63 additions and 18 deletions

View File

@@ -138,7 +138,7 @@ public class InlineModelResolver {
if (m.getAllOf() != null && !m.getAllOf().isEmpty()) {
// check to ensure at least of the allOf item is model
for (Schema inner : m.getAllOf()) {
if (isModelNeeded(inner)) {
if (isModelNeeded(ModelUtils.getReferencedSchema(openAPI, inner))) {
return true;
}
}

View File

@@ -1019,4 +1019,22 @@ public class InlineModelResolverTest {
Schema nothingNew = openAPI.getComponents().getSchemas().get("nothing_new");
assertTrue(nothingNew.getProperties().get("arbitrary_request_body_array_property") instanceof ObjectSchema);
}
@Test
public void resolveInlineRequestBodyAllOf() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/inline_model_resolver.yaml");
new InlineModelResolver().flatten(openAPI);
RequestBody requestBodyReference = openAPI.getPaths().get("/resolve_inline_request_body_allof").getPost().getRequestBody();
assertEquals("#/components/schemas/resolveInlineRequestBodyAllOf_request",
requestBodyReference.getContent().get("application/json").getSchema().get$ref());
//assertEquals("#/components/schemas/resolveInlineRequestBodyAllOf_request", requestBodyReference.get$ref());
ComposedSchema allOfModel =(ComposedSchema) openAPI.getComponents().getSchemas().get("resolveInlineRequestBodyAllOf_request");
assertEquals("#/components/schemas/resolveInlineRequestBody_request", allOfModel.getAllOf().get(0).get$ref());
//Schema allOfModel = ModelUtils.getReferencedSchema(openAPI, requestBodyReference.get$ref());
//RequestBody referencedRequestBody = ModelUtils.getReferencedRequestBody(openAPI, requestBodyReference);
//assertTrue(referencedRequestBody.getRequired());
}
}

View File

@@ -160,7 +160,7 @@ public class GoClientCodegenTest {
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);
TestUtils.assertFileContains(Paths.get(output + "/model_example.go"), "Child NullableChild");
TestUtils.assertFileContains(Paths.get(output + "/model_example.go"), "Child NullableExampleChild");
}
@Test

View File

@@ -160,6 +160,26 @@ paths:
# Swagger parser doesn't use MapSchema currently,
# so we need to set a MapSchema instance as the schema manually for testing.
# see InlineModelResolverTest#resolveInlineMapSchemaInResponse()
/resolve_inline_request_body_allof:
post:
requestBody:
content:
application/json:
schema:
allOf:
- type: object
properties:
name:
type: string
address:
type: object
properties:
street:
type: string
operationId: resolveInlineRequestBodyAllOf
responses:
'200':
description: OK
/arbitrary_object_request_body:
post:
requestBody:

View File

@@ -12,6 +12,7 @@ docs/Apple.md
docs/AppleReq.md
docs/ArrayOfArrayOfNumberOnly.md
docs/ArrayOfInlineAllOf.md
docs/ArrayOfInlineAllOfArrayAllofDogPropertyInner.md
docs/ArrayOfInlineAllOfArrayAllofDogPropertyInnerAllOf.md
docs/ArrayOfNumberOnly.md
docs/ArrayTest.md
@@ -133,6 +134,7 @@ src/main/java/org/openapitools/client/model/Apple.java
src/main/java/org/openapitools/client/model/AppleReq.java
src/main/java/org/openapitools/client/model/ArrayOfArrayOfNumberOnly.java
src/main/java/org/openapitools/client/model/ArrayOfInlineAllOf.java
src/main/java/org/openapitools/client/model/ArrayOfInlineAllOfArrayAllofDogPropertyInner.java
src/main/java/org/openapitools/client/model/ArrayOfInlineAllOfArrayAllofDogPropertyInnerAllOf.java
src/main/java/org/openapitools/client/model/ArrayOfNumberOnly.java
src/main/java/org/openapitools/client/model/ArrayTest.java

View File

@@ -162,6 +162,7 @@ Class | Method | HTTP request | Description
- [AppleReq](docs/AppleReq.md)
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
- [ArrayOfInlineAllOf](docs/ArrayOfInlineAllOf.md)
- [ArrayOfInlineAllOfArrayAllofDogPropertyInner](docs/ArrayOfInlineAllOfArrayAllofDogPropertyInner.md)
- [ArrayOfInlineAllOfArrayAllofDogPropertyInnerAllOf](docs/ArrayOfInlineAllOfArrayAllofDogPropertyInnerAllOf.md)
- [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
- [ArrayTest](docs/ArrayTest.md)

View File

@@ -2181,9 +2181,7 @@ components:
type: string
array_allof_dog_property:
items:
allOf:
- $ref: '#/components/schemas/Dog_allOf'
- $ref: '#/components/schemas/ArrayOfInlineAllOf_array_allof_dog_property_inner_allOf'
$ref: '#/components/schemas/ArrayOfInlineAllOf_array_allof_dog_property_inner'
type: array
required:
- name
@@ -2348,6 +2346,10 @@ components:
color:
type: string
type: object
ArrayOfInlineAllOf_array_allof_dog_property_inner:
allOf:
- $ref: '#/components/schemas/Dog_allOf'
- $ref: '#/components/schemas/ArrayOfInlineAllOf_array_allof_dog_property_inner_allOf'
securitySchemes:
petstore_auth:
flows:

View File

@@ -9,7 +9,7 @@
|------------ | ------------- | ------------- | -------------|
|**id** | **Long** | | [optional] |
|**name** | **String** | | |
|**arrayAllofDogProperty** | [**List&lt;DogAllOf&gt;**](DogAllOf.md) | | [optional] |
|**arrayAllofDogProperty** | [**List&lt;ArrayOfInlineAllOfArrayAllofDogPropertyInner&gt;**](ArrayOfInlineAllOfArrayAllofDogPropertyInner.md) | | [optional] |

View File

@@ -225,6 +225,7 @@ public class JSON {
.registerTypeAdapterFactory(new org.openapitools.client.model.AppleReq.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfArrayOfNumberOnly.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfInlineAllOf.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfInlineAllOfArrayAllofDogPropertyInner.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfInlineAllOfArrayAllofDogPropertyInnerAllOf.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfNumberOnly.CustomTypeAdapterFactory())
.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayTest.CustomTypeAdapterFactory())

View File

@@ -25,8 +25,7 @@ import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.openapitools.client.model.ArrayOfInlineAllOfArrayAllofDogPropertyInnerAllOf;
import org.openapitools.client.model.DogAllOf;
import org.openapitools.client.model.ArrayOfInlineAllOfArrayAllofDogPropertyInner;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -63,7 +62,7 @@ public class ArrayOfInlineAllOf {
public static final String SERIALIZED_NAME_ARRAY_ALLOF_DOG_PROPERTY = "array_allof_dog_property";
@SerializedName(SERIALIZED_NAME_ARRAY_ALLOF_DOG_PROPERTY)
private List<DogAllOf> arrayAllofDogProperty = null;
private List<ArrayOfInlineAllOfArrayAllofDogPropertyInner> arrayAllofDogProperty = null;
public ArrayOfInlineAllOf() {
}
@@ -114,13 +113,13 @@ public class ArrayOfInlineAllOf {
}
public ArrayOfInlineAllOf arrayAllofDogProperty(List<DogAllOf> arrayAllofDogProperty) {
public ArrayOfInlineAllOf arrayAllofDogProperty(List<ArrayOfInlineAllOfArrayAllofDogPropertyInner> arrayAllofDogProperty) {
this.arrayAllofDogProperty = arrayAllofDogProperty;
return this;
}
public ArrayOfInlineAllOf addArrayAllofDogPropertyItem(DogAllOf arrayAllofDogPropertyItem) {
public ArrayOfInlineAllOf addArrayAllofDogPropertyItem(ArrayOfInlineAllOfArrayAllofDogPropertyInner arrayAllofDogPropertyItem) {
if (this.arrayAllofDogProperty == null) {
this.arrayAllofDogProperty = new ArrayList<>();
}
@@ -135,12 +134,12 @@ public class ArrayOfInlineAllOf {
@javax.annotation.Nullable
@ApiModelProperty(value = "")
public List<DogAllOf> getArrayAllofDogProperty() {
public List<ArrayOfInlineAllOfArrayAllofDogPropertyInner> getArrayAllofDogProperty() {
return arrayAllofDogProperty;
}
public void setArrayAllofDogProperty(List<DogAllOf> arrayAllofDogProperty) {
public void setArrayAllofDogProperty(List<ArrayOfInlineAllOfArrayAllofDogPropertyInner> arrayAllofDogProperty) {
this.arrayAllofDogProperty = arrayAllofDogProperty;
}
@@ -273,7 +272,7 @@ public class ArrayOfInlineAllOf {
// validate the optional field `array_allof_dog_property` (array)
for (int i = 0; i < jsonArrayarrayAllofDogProperty.size(); i++) {
DogAllOf.validateJsonObject(jsonArrayarrayAllofDogProperty.get(i).getAsJsonObject());
ArrayOfInlineAllOfArrayAllofDogPropertyInner.validateJsonObject(jsonArrayarrayAllofDogProperty.get(i).getAsJsonObject());
};
}
}

View File

@@ -23,7 +23,7 @@ import com.google.gson.stream.JsonWriter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import org.openapitools.client.model.ArrayOfInlineAllOfArrayAllofDogPropertyItemsAllOf;
import org.openapitools.client.model.ArrayOfInlineAllOfArrayAllofDogPropertyInnerAllOf;
import org.openapitools.client.model.DogAllOf;
import com.google.gson.Gson;

View File

@@ -1936,9 +1936,7 @@ components:
NullableAllOf:
properties:
child:
allOf:
- $ref: '#/components/schemas/NullableAllOfChild'
nullable: true
$ref: '#/components/schemas/NullableAllOf_child'
type: object
OneOfPrimitiveType:
oneOf:
@@ -2108,6 +2106,10 @@ components:
declawed:
type: boolean
type: object
NullableAllOf_child:
allOf:
- $ref: '#/components/schemas/NullableAllOfChild'
nullable: true
securitySchemes:
petstore_auth:
flows: