Skip to content

Commit

Permalink
Remove default ApiResponse (with code '0') from generated swagger ann…
Browse files Browse the repository at this point in the history
…otations
  • Loading branch information
altro3 committed Feb 16, 2024
1 parent 6886c98 commit f4c8af0
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,14 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
param.vendorExtensions.put("hasMultipleParams", hasMultipleParams);
}
op.vendorExtensions.put("originReturnProperty", op.returnProperty);
if (op.responses != null && !op.responses.isEmpty()) {
for (var resp : op.responses) {
if (resp.isDefault) {
resp.code = "default";
}
}
}

processParametersWithAdditionalMappings(op.allParams, op.imports);
processWithResponseBodyMapping(op);
processOperationWithResponseWrappers(op);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,27 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
param.vendorExtensions.put("hasMultipleParams", hasMultipleParams);
}
op.vendorExtensions.put("originReturnProperty", op.returnProperty);
if (op.responses != null && !op.responses.isEmpty()) {
for (var resp : op.responses) {
if (resp.isDefault) {
resp.code = "default";
}
}
}
/*
0 = {LoadedVisitor@8165} "io.micronaut.openapi.visitor.OpenApiGroupInfoVisitor@3d615b2e"
1 = {LoadedVisitor@8166} "io.micronaut.context.visitor.InternalApiTypeElementVisitor@3a1f2a1"
2 = {LoadedVisitor@8167} "io.micronaut.inject.beans.visitor.MapperVisitor@40ce9049"
3 = {LoadedVisitor@8168} "io.micronaut.context.visitor.ExecutableVisitor@52ca2652"
4 = {LoadedVisitor@8169} "io.micronaut.context.visitor.ContextConfigurerVisitor@65c5bf0e"
5 = {LoadedVisitor@8170} "io.micronaut.context.visitor.ConfigurationReaderVisitor@697d6ded"
6 = {LoadedVisitor@8171} "io.micronaut.security.annotation.expressions.SecuredEvaluationContextRegistrar@5d05f453"
7 = {LoadedVisitor@8172} "io.micronaut.validation.visitor.async.AsyncTypeElementVisitor@5efeb117"
8 = {LoadedVisitor@5507} "io.micronaut.openapi.visitor.OpenApiJacksonVisitor@543d5863"
9 = {LoadedVisitor@8173} "io.micronaut.inject.beans.visitor.IntrospectedTypeElementVisitor@160b4809"
10 = {LoadedVisitor@8174} "io.micronaut.context.visitor.BeanImportVisitor@149b4d20"
*/

processParametersWithAdditionalMappings(op.allParams, op.imports);
processWithResponseBodyMapping(op);
processOperationWithResponseWrappers(op);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public static AnnotationMetadata getAnnotationMetadata(Element el) {
if (constructorMetadata == null || constructorMetadata.isEmpty()) {
return propMetadata;
}
return new AnnotationMetadataHierarchy(true, new AnnotationMetadata[] {propMetadata, constructorMetadata});
return new AnnotationMetadataHierarchy(true, propMetadata, constructorMetadata);
}
return el.getAnnotationMetadata();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ open class Animal (
@Size(max = 50)
@Nullable
@Schema(name = "class", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
// @JsonProperty(JSON_PROPERTY_PROPERTY_CLASS)
@JsonProperty(JSON_PROPERTY_PROPERTY_CLASS)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var propertyClass: String? = null,
) {
Expand Down Expand Up @@ -264,7 +264,7 @@ class MyBean {}
Utils.testReference != null

when: "The OpenAPI is retrieved"
OpenAPI openAPI = Utils.testReference
def openAPI = Utils.testReference
Schema schema = openAPI.components.schemas.Animal

then: "the components are valid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,4 +929,143 @@ class MyBean {}
dtoSchema.properties.zone_id.type == 'string'
}

void "test annotations on constructor parameters level"() {

when:
buildBeanDefinition('test.MyBean', '''
package test;
import java.math.BigDecimal;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Put;
import io.micronaut.serde.annotation.Serdeable;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import reactor.core.publisher.Mono;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@Controller
class HelloController {
@Put("/sendModelWithDiscriminator")
Mono<Animal> sendModelWithDiscriminator(
@Body @NotNull @Valid Animal animal
) {
return Mono.empty();
}
}
@Serdeable
@JsonIgnoreProperties(
value = "class", // ignore manually set class, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the class to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "class", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Bird.class, name = "ave"),
})
class Animal {
@JsonProperty("class")
protected String propertyClass;
@Schema(name = "color", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@Nullable
private ColorEnum color;
public String getPropertyClass() {
return propertyClass;
}
public void setPropertyClass(String propertyClass) {
this.propertyClass = propertyClass;
}
public ColorEnum getColor() {
return color;
}
public void setColor(ColorEnum color) {
this.color = color;
}
}
@Serdeable
class Bird extends Animal {
private Integer numWings;
@DecimalMax("123.78")
private BigDecimal beakLength;
private String featherDescription;
Bird(
@Min(10) Integer numWings,
@JsonProperty("myLength") BigDecimal beakLength,
String featherDescription) {
}
public Integer getNumWings() {
return numWings;
}
public void setNumWings(Integer numWings) {
this.numWings = numWings;
}
public BigDecimal getBeakLength() {
return beakLength;
}
public void setBeakLength(BigDecimal beakLength) {
this.beakLength = beakLength;
}
public String getFeatherDescription() {
return featherDescription;
}
public void setFeatherDescription(String featherDescription) {
this.featherDescription = featherDescription;
}
}
@Serdeable
enum ColorEnum {
@JsonProperty("red")
RED
}
@jakarta.inject.Singleton
class MyBean {}
''')
then: "the state is correct"
Utils.testReference != null

when: "The OpenAPI is retrieved"
def openApi = Utils.testReference
def schemas = openApi.components.schemas

then: "the components are valid"
schemas.Animal
schemas.Bird
schemas.ColorEnum

!schemas.Bird.allOf[1].properties.beakLength
schemas.Bird.allOf[1].properties.myLength
schemas.Bird.allOf[1].properties.myLength.maximum == 123.78
schemas.Bird.allOf[1].properties.numWings.minimum == 10
}

}

0 comments on commit f4c8af0

Please sign in to comment.