Open jaaufauvre opened 4 years ago
Ran into the exact same issue, but when using a multipart request as follows:
post:
tags:
- Details
summary: Post details
operationId: postDetails
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
details:
$ref: '#/components/schemas/detailsV1'
file:
type: string
format: binary
required:
- details
- file
This will generate:
@Override
public ResponseEntity<ResponseV1> postDetails(DetailsV1 details, @Valid MultipartFile file) {
return null;
}
Which will be missing the '@Valid' annotation for the JSON object.
Still seeing this issue with OpenAPI Generator 5.2.0
We have just encountered this issue (or a very closely related one) via the Maven plugin, openapi-generator-maven-plugin
v5.1.1 & 5.2.0 - Spring generator. We are missing the @Pattern
annotation (and example
attribute in @ApiModelProperty
) on the agreementID
property in our generated AgreementDetails
class, where pattern is specified on the referenced base simple type AgreementId
, e.g.
AgreementId:
type: string
pattern: RM[0-9]{4}(.[0-9]{1,2})?
example: RM1234
readOnly: true
AgreementDetails:
type: object
properties:
agreementID:
allOf:
- $ref: '#/components/schemas/AgreementId'
readOnly: false
I looked into this quickly yesterday. It's definitely not a template issue, the input model of the template is already lacking the values for the limits. We have some models in our code base that have this issue and other seemingly identical models that work perfectly fine.
Initially I also assumed that this could be related to allOf, however none of our affected models are used directly as part of an allOf and not even the containing models are part of an allOf directly.
Some extra info:
ModelAllOf[N].java
outputs)Quick hack could be to make DefaultCodegen.unaliasSchema
handle allOf(single $ref)
specifically. This won't help for combining constraints on primitive types, but that also currently doesn't work for allOf(several object types)
After unaliasSchema
goes ModelUtils.syncValidationProperties
and sees schema instanceOf ComposedSchema
which doesn't show constraints by itself.
ComposedSchema.getType() == null
so DefaultCodegen.fromProperty
calls into DefaultCodegen.getSchemaType
and gets the primitive type out. Notably to get that it traverses allOf options and can see constraints, also there it picks just one branch.
Looks like a better fix could be making ModelUtils.syncValidationProperties
join constraints in allOf ComposedSchema (and maybe also find minimal common constraints in oneOf/anyOf case)
As a quick workaround, extending generator (tried with JavaClientCodegen) with
@Override
public CodegenProperty fromProperty(String name, Schema p) {
CodegenProperty result = super.fromProperty(name, p);
syncValidationProperties(p, result);
return result;
}
private void syncValidationProperties(Schema schema, IJsonSchemaValidationProperties target) {
if (!(schema instanceof ComposedSchema) || target == null) {
return;
}
// rely on ModelUtils.syncValidationProperties not re-applying empty constraints
// don't handle allOf (min=2, min=1), the last one wins for now
Optional.ofNullable(((ComposedSchema) schema).getAllOf())
.orElse(Collections.emptyList())
.stream()
.map(p -> ModelUtils.unaliasSchema(openAPI, p, importMapping))
.forEach(p -> ModelUtils.syncValidationProperties(p, target));
}
seems to work for some cases at least
for newer versions workaround needs to use new overload
@Override
public CodegenProperty fromProperty(String name, Schema p, boolean required, boolean schemaIsFromAdditionalProperties) {
CodegenProperty result = super.fromProperty(name, p, required, schemaIsFromAdditionalProperties);
syncValidationProperties(p, result);
return result;
}
/**
* Workaround for https://github.com/OpenAPITools/openapi-generator/issues/7342
* extended version of
* {@link org.openapitools.codegen.utils.ModelUtils#syncValidationProperties(Schema, IJsonSchemaValidationProperties)}
*/
private void syncValidationProperties(@Nullable Schema schema,
@Nullable IJsonSchemaValidationProperties target) {
if (!(schema instanceof ComposedSchema) || target == null) {
return;
}
// rely on ModelUtils.syncValidationProperties not re-applying empty constraints
// don't handle allOf (min=2, min=1), the last one wins for now
Optional.ofNullable(((ComposedSchema) schema).getAllOf())
.orElse(Collections.emptyList())
.stream()
.map(p -> ModelUtils.unaliasSchema(openAPI, p, importMapping))
.forEach(p -> ModelUtils.syncValidationProperties(p, target));
}
Bug Report Checklist
Description
When using:
The generated code is:
Instead of:
openapi-generator version
OpenAPI Generator 4.3.1 and 5.0.0-alpha.
OpenAPI declaration file content or url
Generation Details
Plugin configuration:
Steps to reproduce
mvn clean generate-sources