forked from loafle/openapi-generator-original
* #12445 - avoid resolving ref schema to the actual schema for enum * #12445 - update samples
This commit is contained in:
@@ -4623,7 +4623,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
// $ref (e.g. #components/schemas/Pet) to determine whether it's a model
|
||||
prop = fromProperty(parameter.getName(), parameterSchema);
|
||||
} else if (getUseInlineModelResolver()) {
|
||||
prop = fromProperty(parameter.getName(), ModelUtils.getReferencedSchema(openAPI, parameterSchema));
|
||||
prop = fromProperty(parameter.getName(), getReferencedSchemaWhenNotEnum(parameterSchema));
|
||||
} else {
|
||||
prop = fromProperty(parameter.getName(), parameterSchema);
|
||||
}
|
||||
@@ -4671,7 +4671,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
if (getUseInlineModelResolver() && !(this instanceof RustServerCodegen)) {
|
||||
// for rust server, we cannot run the following as it uses
|
||||
// $ref (e.g. #components/schemas/Pet) to determine whether it's a model
|
||||
parameterSchema = ModelUtils.getReferencedSchema(openAPI, parameterSchema);
|
||||
parameterSchema = getReferencedSchemaWhenNotEnum(parameterSchema);
|
||||
}
|
||||
|
||||
ModelUtils.syncValidationProperties(parameterSchema, codegenParameter);
|
||||
@@ -4848,6 +4848,14 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
return codegenParameter;
|
||||
}
|
||||
|
||||
private Schema getReferencedSchemaWhenNotEnum(Schema parameterSchema) {
|
||||
Schema referencedSchema = ModelUtils.getReferencedSchema(openAPI, parameterSchema);
|
||||
if (referencedSchema.getEnum() != null && !referencedSchema.getEnum().isEmpty()) {
|
||||
referencedSchema = parameterSchema;
|
||||
}
|
||||
return referencedSchema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data type of parameter.
|
||||
* Returns null by default to use the CodegenProperty.datatype value
|
||||
|
||||
@@ -69,6 +69,16 @@ public class DefaultCodegenTest {
|
||||
Assert.assertEquals(Sets.intersection(operation.imports, Sets.newHashSet("Person")).size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumImports() {
|
||||
final DefaultCodegen codegen = new DefaultCodegen();
|
||||
final OpenAPI openApi = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_12445.yaml");
|
||||
codegen.setOpenAPI(openApi);
|
||||
PathItem path = openApi.getPaths().get("/pets/petType/{type}");
|
||||
CodegenOperation operation = codegen.fromOperation("/pets/petType/{type}", "get", path.getGet(), path.getServers());
|
||||
Assert.assertEquals(Sets.intersection(operation.imports, Sets.newHashSet("PetByType")).size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasBodyParameter() {
|
||||
final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet");
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
openapi: 3.0.1
|
||||
info:
|
||||
version: 1.0.0
|
||||
title: Example
|
||||
license:
|
||||
name: MIT
|
||||
servers:
|
||||
- url: http://api.example.xyz/v1
|
||||
paths:
|
||||
/pets/petType/{type}:
|
||||
get:
|
||||
parameters:
|
||||
- name: 'type'
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/components/schemas/PetByType'
|
||||
responses:
|
||||
'200':
|
||||
description: 'get by type'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Pet'
|
||||
|
||||
components:
|
||||
schemas:
|
||||
Pet:
|
||||
type: object
|
||||
properties:
|
||||
petType:
|
||||
$ref: '#/components/schemas/PetByType'
|
||||
required:
|
||||
- petType
|
||||
Dog:
|
||||
allOf:
|
||||
# This field will not match to any type.
|
||||
- description: Dog information
|
||||
- $ref: '#/components/schemas/Pet'
|
||||
- type: object
|
||||
properties:
|
||||
bark:
|
||||
type: boolean
|
||||
breed:
|
||||
type: string
|
||||
enum: [Dingo, Husky, Retriever, Shepherd]
|
||||
Cat:
|
||||
allOf:
|
||||
- $ref: '#/components/schemas/Pet'
|
||||
- type: object
|
||||
properties:
|
||||
hunts:
|
||||
type: boolean
|
||||
age:
|
||||
type: integer
|
||||
PetByType:
|
||||
type: string
|
||||
enum: [Cat, Dog]
|
||||
@@ -37,9 +37,9 @@ export interface FakeEnumRequestGetInlineRequest {
|
||||
}
|
||||
|
||||
export interface FakeEnumRequestGetRefRequest {
|
||||
stringEnum?: FakeEnumRequestGetRefStringEnumEnum;
|
||||
stringEnum?: StringEnum;
|
||||
nullableStringEnum?: StringEnum | null;
|
||||
numberEnum?: FakeEnumRequestGetRefNumberEnumEnum;
|
||||
numberEnum?: NumberEnum;
|
||||
nullableNumberEnum?: NumberEnum | null;
|
||||
}
|
||||
|
||||
@@ -210,21 +210,3 @@ export const FakeEnumRequestGetInlineNumberEnumEnum = {
|
||||
NUMBER_3: 3
|
||||
} as const;
|
||||
export type FakeEnumRequestGetInlineNumberEnumEnum = typeof FakeEnumRequestGetInlineNumberEnumEnum[keyof typeof FakeEnumRequestGetInlineNumberEnumEnum];
|
||||
/**
|
||||
* @export
|
||||
*/
|
||||
export const FakeEnumRequestGetRefStringEnumEnum = {
|
||||
One: 'one',
|
||||
Two: 'two',
|
||||
Three: 'three'
|
||||
} as const;
|
||||
export type FakeEnumRequestGetRefStringEnumEnum = typeof FakeEnumRequestGetRefStringEnumEnum[keyof typeof FakeEnumRequestGetRefStringEnumEnum];
|
||||
/**
|
||||
* @export
|
||||
*/
|
||||
export const FakeEnumRequestGetRefNumberEnumEnum = {
|
||||
NUMBER_1: 1,
|
||||
NUMBER_2: 2,
|
||||
NUMBER_3: 3
|
||||
} as const;
|
||||
export type FakeEnumRequestGetRefNumberEnumEnum = typeof FakeEnumRequestGetRefNumberEnumEnum[keyof typeof FakeEnumRequestGetRefNumberEnumEnum];
|
||||
|
||||
@@ -37,9 +37,9 @@ export interface FakeEnumRequestGetInlineRequest {
|
||||
}
|
||||
|
||||
export interface FakeEnumRequestGetRefRequest {
|
||||
stringEnum?: FakeEnumRequestGetRefStringEnumEnum;
|
||||
stringEnum?: StringEnum;
|
||||
nullableStringEnum?: StringEnum | null;
|
||||
numberEnum?: FakeEnumRequestGetRefNumberEnumEnum;
|
||||
numberEnum?: NumberEnum;
|
||||
nullableNumberEnum?: NumberEnum | null;
|
||||
}
|
||||
|
||||
@@ -210,21 +210,3 @@ export enum FakeEnumRequestGetInlineNumberEnumEnum {
|
||||
NUMBER_2 = 2,
|
||||
NUMBER_3 = 3
|
||||
}
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum FakeEnumRequestGetRefStringEnumEnum {
|
||||
One = 'one',
|
||||
Two = 'two',
|
||||
Three = 'three'
|
||||
}
|
||||
/**
|
||||
* @export
|
||||
* @enum {string}
|
||||
*/
|
||||
export enum FakeEnumRequestGetRefNumberEnumEnum {
|
||||
NUMBER_1 = 1,
|
||||
NUMBER_2 = 2,
|
||||
NUMBER_3 = 3
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user