-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better support for inline schemas in parameters #12369
Changes from 6 commits
eedc6f5
195f1da
95ca392
2b10b64
559409d
33a1b6b
9348b15
ced1d53
5ffc8c8
f531f37
b7aa873
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4575,8 +4575,14 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) | |
} | ||
|
||
Schema parameterSchema; | ||
|
||
// the parameter model name is obtained from the schema $ref | ||
// e.g. #/components/schemas/list_pageQuery_parameter => toModelName(list_pageQuery_parameter) | ||
String parameterModelName = null; | ||
|
||
if (parameter.getSchema() != null) { | ||
parameterSchema = parameter.getSchema(); | ||
parameterModelName = getParameterDataType(parameter ,parameter.getSchema()); | ||
parameterSchema = ModelUtils.getReferencedSchema(openAPI, parameter.getSchema()); | ||
CodegenProperty prop = fromProperty(parameter.getName(), parameterSchema); | ||
codegenParameter.setSchema(prop); | ||
} else if (parameter.getContent() != null) { | ||
|
@@ -4586,7 +4592,8 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) | |
} | ||
Map.Entry<String, MediaType> entry = content.entrySet().iterator().next(); | ||
codegenParameter.contentType = entry.getKey(); | ||
parameterSchema = entry.getValue().getSchema(); | ||
parameterModelName = getParameterDataType(parameter, entry.getValue().getSchema()); | ||
parameterSchema = ModelUtils.getReferencedSchema(openAPI, entry.getValue().getSchema()); | ||
} else { | ||
parameterSchema = null; | ||
} | ||
|
@@ -4717,9 +4724,8 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) | |
//} | ||
//codegenProperty.required = true; | ||
|
||
String parameterDataType = this.getParameterDataType(parameter, parameterSchema); | ||
if (parameterDataType != null) { | ||
codegenParameter.dataType = parameterDataType; | ||
if (parameterModelName != null) { | ||
codegenParameter.dataType = parameterModelName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should codegenParameters handle dataType and complexType differently than codegenProperty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change above simply uses a different (better) name in the code block as |
||
if (ModelUtils.isObjectSchema(parameterSchema)) { | ||
codegenProperty.complexType = codegenParameter.dataType; | ||
} | ||
|
@@ -4790,17 +4796,17 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) | |
} | ||
|
||
/** | ||
* Returns the data type of a parameter. | ||
* Returns the data type of parameter. | ||
* Returns null by default to use the CodegenProperty.datatype value | ||
* | ||
* @param parameter Parameter | ||
* @param schema Schema | ||
* @return data type | ||
*/ | ||
protected String getParameterDataType(Parameter parameter, Schema schema) { | ||
if (parameter.get$ref() != null) { | ||
String refName = ModelUtils.getSimpleRef(parameter.get$ref()); | ||
return toModelName(refName); | ||
Schema unaliasSchema = ModelUtils.unaliasSchema(openAPI, schema); | ||
if (unaliasSchema.get$ref() != null) { | ||
return toModelName(ModelUtils.getSimpleRef(unaliasSchema.get$ref())); | ||
} | ||
return null; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -725,14 +725,33 @@ public static boolean isModel(Schema schema) { | |
} | ||
|
||
// has properties | ||
if (null != schema.getProperties()) { | ||
if (null != schema.getProperties() && !schema.getProperties().isEmpty()) { | ||
return true; | ||
} | ||
|
||
// composed schema is a model, consider very simple ObjectSchema a model | ||
return schema instanceof ComposedSchema || schema instanceof ObjectSchema; | ||
} | ||
|
||
/** | ||
* Check to see if the schema is a model with properties only (non-composed model) | ||
* | ||
* @param schema potentially containing a '$ref' | ||
* @return true if it's a model with at least one properties | ||
*/ | ||
public static boolean isModelWithPropertiesOnly(Schema schema) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
How about limiting this to type object and AnyType models only? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. I previously didn't check I simply want to set the isModel flag correctly because now isMap is set to true for a simple model. I've not touched the code in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me look into what is happening with python experimental. I think it is changing because the component schema changed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Root cause investigation is here: https://github.com/OpenAPITools/openapi-generator/pull/12369/files#r876380534 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be true if the schema also has anyOf or allOf definitions? |
||
if (schema == null) { | ||
return false; | ||
} | ||
|
||
if (null != schema.getProperties() && !schema.getProperties().isEmpty() && // has properties | ||
(schema.getAdditionalProperties() == null || // no additionalProperties is set | ||
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean)schema.getAdditionalProperties()))) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean hasValidation(Schema sc) { | ||
return ( | ||
sc.getMaxItems() != null || | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something odd is happening with unaliasSchema
The original code extracts the schema, unaliases it, then uses that unaliased schemas to set properties.
This code instead gets the schema from the ref automatically.
So why is unaliasSchema not handling this case automatically originally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see my branch to fix the python-experimental issue:
https://github.com/OpenAPITools/openapi-generator/pull/12397/files
unaliasSchema is not retuning the actual enum schema because if an schema contains a ref to an enum schema, unaliasSchmema returns that schema with the $ref in it. That assumes that the generator will generate a model reference to the enum schema. Options are:
ModelUtils.getReferencedSchema
to only run when getUseInlineModelResolver() is true + the ref is to an enum schema. Also how about doing this logic after the schema has already been unaliased?One useful related question is: Do we have any flag for whether or not a generator generates models for enums?
I don't think that we do. If we had one we could use it in unaliasSchema
That would be a great general solution here.